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

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
Email: Tim@apple.com
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.

Now the variable classInstance is of the type MyClass.

__init__

_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.

ttk.Style()

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

style.configure()

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

ttk.Frame()

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

.pack()

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.

PhotoImage()

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

.Label()

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.

.grid()

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.

.Entry()

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

Text()

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

.Button()

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.

messagebox.showinfo

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

Tk()

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

.geometry()

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

Further Resources