Tkinter: UI for python apps

Technocrat
CoderHack.com
Published in
3 min readSep 14, 2023
Photo by Tirza van Dijk on Unsplash

Tkinter is Python’s de-facto standard GUI (Graphical User Interface) package. It wraps Tk, a popular and powerful cross-platform GUI toolkit. Tkinter is bundled with Python, so you already have it installed if you have Python on your system.

Tkinter was created in the early 1990s and has been bundled with Python for over 20 years. It provides a robust and platform independent windowing toolkit, that is available for most operating systems, including Linux, Windows, and macOS.

Creating your first window

To get started with Tkinter, you first need to import it:

from tkinter import *

Then you create a Tk root window:

root = Tk()

You can set the title, size, and background color of the window like this:

root.title("My Window")
root.geometry("640x480")
root.config(bg="lightblue")

To add widgets like buttons, text, and images to the window, use the pack() geometry manager:

button = Button(root, text="Click Me!")
button.pack()

Finally, you enter the event loop using mainloop():

mainloop()

This listens for events like button clicks and keeps the window open.

Adding Widgets

Tkinter has a variety of widgets available like:

  • Buttons
  • Text
  • Labels
  • Radiobuttons
  • Checkbuttons
  • Entries
  • And many more!

You can create a button like this:

button = Button(root, text="Click Me!")
button.pack()

A label with some text:

label = Label(root, text="Hello World!")
label.pack()

And an entry (text input) like this:

entry = Entry(root)
entry.pack()

You can configure options like background color, font, size, and more for each widget. Refer to the Tkinter documentation for all available widgets and their options.

Layout Managers

Tkinter has two geometry managers (layout managers): pack() and grid().

The pack() manager pushes widgets against each other in the window and is very simple to use, but is limited to stacking widgets either vertically or horizontally.

The grid() manager organizes widgets in a 2-D table-like structure and offers more flexibility. You specify the row, column, rowspan, and columnspan options to control the widget's position in the layout.

Here’s an example of using grid():

label = Label(root, text="First Name: ")    
label.grid(row=0, column=0)

entry = Entry(root)
entry.grid(row=0, column=1)

label2 = Label(root, text="Last Name: ")
label2.grid(row=1, column=0)

entry2 = Entry(root)
entry2.grid(row=1, column=1)

This will put the two labels in column 0, and the two entries in column 1, with two rows.

Event Handling

To handle events like mouse clicks, you need to bind event handlers to widgets. For example, to handle a button click, you’d do:

def click(): 
print("Button clicked!")

button = Button(root, text="Click Me!", command=click)
button.pack()

Here we bind the click() function to the command option of the button.

For keyboard events, you use bind():

def press(event): 
print(f"You pressed {event.key}!")

root.bind("<KeyPress>", press)

This will call the press() function whenever any key is pressed while the window is focused.

Conclusion

This covers the basics of building GUIs in Python using Tkinker. There are many additional topics like theming, menus, canvas, layout helpers, and more to explore. The official Tk documentation contains a wealth of additional information and resources for learning Tkinter in depth.

Tkinter is a basic but useful GUI toolkit that allows you to build simple yet nice interfaces for your Python applications. I hope this helps you get started with creating your own GUIs in Python!

--

--