Python Tkinter Introduction

Python Tkinter Introduction

Tkinter is the well-known framework for Python GUI Development. Tkinter allows developers to build graphical user interfaces in Python that look great, and run on all major operating systems. When you install the latest version of Python, you are also getting Tkinter included by default with that installation. This means it is as easy as adding a few import statements to the top of your code to begin leveraging the power that Tkinter offers. Another nice feature of Tkinter is that it includes themed GUI elements that maintain a consistent look and feel with those different operating systems. Let’s see if we can get a few simple programs built with Tkinter now.

Here we are creating a basic GUI window with Tkinter. We can see it only takes a few lines of code to get a Window to display on our system. The Tk() constructor method is used to create a top-level widget. This is the main window for a Tkinter application. We store that reference in the root variable. Then a Label() is created with the text of “Welcome to Tkinter :-)”. This label is a child of the root window. Notice the pack() method. This is the geometry management method that auto-calculates how to place widgets within your application. Lastly, the mainloop() method is called and we get our first GUI in Python!

tkinter root widget


Create A New Tkinter Widget

Widgets in a Tk application are all of the components that you see inside of the main window. They act as controls to work with the program, like text entry fields, buttons, and labels. Widgets are also used to display information to the user and each of these different types of widgets is defined as a class that is part of the Tkinter package. A widget in a Tkinter GUI is an instance of that class. That widget object stores all of the data and behavior associated with that widget. These widgets all belong to a hierarchy with the main window, in our case root, at the top of the hierarchy. Any time you add a widget in Tkinter, a parent must be specified.

Tkinter widget layout

Using that knowledge, we can add a button to our GUI with some text on it. Note, this button does not yet do anything, but we can configure event handlers soon. Also note that the main window os the GUI is fully resizable, and can be maximized, minimized, and shaped however you like. This is just like most desktop applications on a PC or Mac.

tkinter button widget

Handle Button Clicks In Tkinter

Now that we know how to add a button to the GUI, let’s see how we can make something happen when a user clicks the button. The goal of this application will be to display two buttons in the GUI. Clicking the first button will display a random even number between 1 and 20. Clicking the second button will display a random odd number between 1 and 20. Let’s see the code to make this work.

So that is a pretty neat button demo. There are a few new concepts here. Firstly, the small application was created as a class so we now have an object-oriented application. The other new concepts are ttk, grid, and command for buttons.

  • Tkinter ttk: These are Tk themed widgets. They are great since they allow the application to look good whether it is run on a Windows, Mac, or Linux machine.
  • Tk grid: The .grid() method is an alternative to using .pack() to place widgets on within the GUI.
  • command callback: You can pass a second argument to a widget constructor and this is the callback that will be executed. For example with a Button widget, the first argument sets the text of the button, and the second argument determines what happens when it is clicked.

Tkinter Widget Configuration

The widgets in a Tkinter application are highly configurable. We’re going to look at the Label widget, as it is commonly used to display information to the user of the application. They can be used to display some text or even an image. This offers visual feedback to the user. Here is an example of a Label widget with both text and an image.

tkinter label text and image

In the code above, we can see that there are several options for configuration like text, wraplength, justify, foreground, background, font, and so on. Put the code above into your own code editor and change the values for the configurations to see the result.


More about Tkinter


Python Tkinter Introduction Summary

In this tutorial, we learned a little bit about the most popular GUI framework for Python, and that is Tkinter. Tkinter makes it easy to go from just having command-line scripts in Python, to providing a graphical user interface to your users where they can interact with Text fields, buttons, and labels to get input and output from the program.