Tkinter Simple Comment App

Tkinter Simple Comment App

In this tutorial, we’ll use Tkinter to build a simple GUI application that gets information from a user, and then outputs the data to the Python console. It is a type of form application and is similar to how you build an Html form to collect information on a website. This version, however, is strictly built as a graphical user interface using Tkinter and Python. This ties together some of the concepts we have been learning about like Tkinter Widgets, Labels, Buttons, and event handling. Let’s have a look at the code.

Python Source Code

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

class Feedback:

    def __init__(self, mainframe):
        mainframe.title('Add Your Comment')
        mainframe.resizable(False, False)
        mainframe.configure(background='#f7f7f7') = ttk.Style()'TFrame', background='#f7f7f7')'TButton', background='#e1d8b9')'TLabel', background='#f7f7f7', font=('Arial', 12))'Header.TLabel', font=('Arial', 18, 'bold'))

        self.header_frame = ttk.Frame(mainframe)

        self.logo = PhotoImage(file='python_logo.gif')
        ttk.Label(self.header_frame, image=self.logo).grid(row=0, column=0, rowspan=2)
        ttk.Label(self.header_frame, text='Comment App', style='Header.TLabel').grid(row=0, column=1)
        ttk.Label(self.header_frame, wraplength=300,
                      'Add your name, email, and comment, then click submit to add your comment.  Click clear if you make a mistake.')).grid(
            row=1, column=1)

        self.content_in_frame = ttk.Frame(mainframe)

        ttk.Label(self.content_in_frame, text='Name:').grid(row=0, column=0, padx=5, sticky='sw')
        ttk.Label(self.content_in_frame, text='Email:').grid(row=0, column=1, padx=5, sticky='sw')
        ttk.Label(self.content_in_frame, text='Comments:').grid(row=2, column=0, padx=5, sticky='sw')

        self.comment_name = ttk.Entry(self.content_in_frame, width=24, font=('Arial', 10))
        self.comment_email = ttk.Entry(self.content_in_frame, width=24, font=('Arial', 10))
        self.comments = Text(self.content_in_frame, width=50, height=10, font=('Arial', 10))

        self.comment_name.grid(row=1, column=0, padx=5)
        self.comment_email.grid(row=1, column=1, padx=5)
        self.comments.grid(row=3, column=0, columnspan=2, padx=5)

        ttk.Button(self.content_in_frame, text='Submit',
                   command=self.submit).grid(row=4, column=0, padx=5, pady=5, sticky='e')
        ttk.Button(self.content_in_frame, text='Clear',
                   command=self.clear).grid(row=4, column=1, padx=5, pady=5, sticky='w')

    def submit(self):
        print(f'Name: {self.comment_name.get()}')
        print(f'Email: {self.comment_email.get()}')
        print(f'Comments: {self.comments.get(1.0, "end")}')
        messagebox.showinfo(title='Comment info', message='Thanks for your comment!')

    def clear(self):
        self.comment_name.delete(0, 'end')
        self.comment_email.delete(0, 'end')
        self.comments.delete(1.0, 'end')

def main():
    root = Tk()
    feedback = Feedback(root)

if __name__ == '__main__': main()

tkinter comment application

Once you submit the comment, you get a nice alert and the comment information is output to the console.
comment alert tkinter

And now we see the output in the console here.

Name: Tim
Comments: This is a cool comment application!

How It Works

The application makes use of the following concepts to do its job.

Python ttk

Tk themed widgets that offer a cross-platform look and feel.

tkinter messagebox

The tkinter.messagebox module provides a template base class and convenience methods for commonly used configurations. The message boxes are modal and will return a subset of (True, False, OK, None, Yes, No) based on the selection of the user.

Python class

The comment application is built as a class in python.

A note about classes and types:
Every variable always has a type in Python. A variable can not be created without assigning it a type. Interestingly in Python, we do not have to declare the type beforehand, as you would need to do in the C programming language. Python is smart enough to infer the type. Using Python, programmers can create their own classes using the class keyword instead of the def keyword. In order to assign the class to a variable, we first have to create an instance of our class. We create the instance and assign this instance to our variable.

class MyClass(object):
    print('Hello from MyClass')

classInstance = MyClass()

Now the variable classInstance is of the type MyClass.


_init__ is a special Python method that is called every time a new object is instantiated from a class. The main purpose of __init__ is to set the initial values of instance members for the new object. Most of the heavy lifting for our comment application is done during the instantiation.


The Style class is used to manipulate the style database, and we made use of it to get the look and feel shown.


Once a Style object is created, you use the .configure() method to apply color, background, etc…


This class is a Simple container widget used to create containers in the application.


The pack method is how we attach widgets into the application. It sizes the frame so that all its contents are at their preferred sizes.


The PhotoImage class is used to display images in labels, buttons, canvases, and text widgets.


A Label is a Tkinter Widget class, that is used to show text or an image. It is a widget that the user just views but not interact with.


The grid() geometry manager organizes widgets in the parent widget. The master widget is split into rows and columns, and each part of the grid can hold a widget. You can use the properties of column, columnspan, ipadx, ipady, padx, pady, row, rowspan and sticky for placement.


To accept single-line text strings from a user you can use the Entry widget.


The Text Widget allows you to display multiple lines of text that can be edited


The Button class was used in this application for both the Submit and Clear functions.

command callback

In order for something to happen when a user clicks a button, you must specify a callback function.

F string

F strings in Python are a wonderful new feature that allows programmers to forego the more verbose format() method.


We used the messagebox.showinfo() method to display some text to the user.


This is the main constructure to launch a GUI using Tkinter.


You can use this method for more precise control over the layout of widgets.

Further Resources