Python Tutorial: How to create a Python GUI using Tkinter

The following is a short article on the basics of creating graphical user interfaces (GUI) in Python by leveraging Tkinter (interface to the Tk GUI toolkit). We’ll cover fundamentals such as creating a brand new window, centring the window on the screen, sizing it to desired dimensions, as well as giving the window a title and configuring it with its own icon. We also cover the basics of placing a new label and button on the window and wiring a new function that changes the text of our label with each button click.

Here is a video that demonstrates the entire process:

History of Tkinter

In 1988, the Tcl (Tool Command Language) programming language was created. It was cross-platform, dynamic, open source. However, it was missing GUI capabilities, so in 1991 a new Tk extension came to Tcl, enabling programmers to build GUIs (graphical user interfaces) natively in Tcl. That’s the reason you see Tcl often written as Tcl/Tk. Now, Tk was awesome, as it provided Tcl developers with all of the basic widgets such as canvas, textbox, button, label, etc. , needed for the development of native look/feel apps in Windows, Linux, Mac OS, Unix. At that time, we’re talking pre-year-2000, Python was also in the stage where it was mostly used without GUI. So, in 1999, Fredrik Lundh wrote a Tkinter (TK + Interface) as a Python binding/interface to the Tk GUI toolkit and shortly after the year 2000, Tkinter became so popular that now it’s de facto a standard Python GUI package. As of Python 3+, Tkinter is actually shipped as part of the Linux, Windows and Mac OS installs of Python.

How to create GUI (a new window) using Tkinter

Here is the code with explanation:

Initialize Tkinter:

import tkinter

Initialize the main window

window = tkinter.Tk()

Size windows to 400 x 400px

window.minsize(400, 400)

Give a window a new title

window.title("Application Name")

Set Icon

window.wm_iconbitmap("icon_name.ico")

Show Window is done through the following command:

window.mainloop()

 

Centering Window on the Screen

Following is the function to find the screen dimensions, calculate the center and set geometry:

def center(win):
# Call all pending idle tasks - carry out geometry management and redraw widgets.
win.update_idletasks()
# Get width and height of the screen
width = win.winfo_width()
height = win.winfo_height()
# Calculate geometry
x = (win.winfo_screenwidth() // 2) - (width // 2)
y = (win.winfo_screenheight() // 2) - (height // 2)
# Set geometry
win.geometry('{}x{}+{}+{}'.format(width, height, x, y))

To center the window on the screen, simply call your function like this:

center(window)

 

Placing Label and Button on the Window

Now, let’s place a ‘My Label’ label on the window at coordinates x=10 and y=10 (top right-hand corner)

label = tkinter.Label(window, text="Click the Button")
label.place(x=10, y=10)

Placing Button uses a similar approach:

button = tkinter.Button(window, text="Button")
button.place(x=10, y=40)

 

Handling a Button Click

Now, let’s create a new function that changes the text of our label with each button click, increasing the counter with each press of the button.

We need to create a new ‘counter’ variable outside of our function first:

counter = 0

Define button press function

def press():
# use globally set counter variable
global counter
# count each button press
counter = counter + 1
# set label
label.config(text=f"Button clicked: {counter} times")

Then add the function to the button command:

button = tkinter.Button(window, text="Button", command=press)

Now, whenever we press the button, we’ll see the label changed and our counter increasing

 

Source Code

The source code is on Github: https://github.com/JozefJarosciak/PythonWindowsAppTkinterExample/blob/master/tkapp.py

And also here:

import tkinter
# 1988 - A new Tool Command programming language was created, called Tcl. It was cross-platform, dynamic, open source and gained pop.
# 1991 - Tk extension came to Tcl, enabling building of GUIs (graphical user interfaces) natively in Tcl (Tcl/TK).
# Tk provided all basic widgets (canvas, textbox, button, label) needed for the development of native look/feel apps in Windows, Linux, Mac OS, Unix.
# 1999 - Fredrik Lundh wrote a Tkinter (TK + Interface) as a Python binding/interface to the Tk GUI toolkit.
# 2000 - Tkinter became de facto a standard GUI. In Python 3+, Tkinter is shipped as part of Linux, Windows and Mac OS X installs of Python.
# Initialize the main window
window = tkinter.Tk()
# Size windows to 400 x 400px
window.minsize(400, 400)
# Give your window a title
window.title("Application Name")
# Set Icon
window.wm_iconbitmap("pacman.ico")
# Place 'Hello World' label on the window
label = tkinter.Label(window, text="Click the Button")
# Place label at coordinates x=10 and y=10 (top right hand corner)
label.place(x=10, y=10)
# Function to find the screen dimensions, calculate the center and set geometry
def center(win):
# Call all pending idle tasks - carry out geometry management and redraw widgets.
win.update_idletasks()
# Get width and height of the screen
width = win.winfo_width()
height = win.winfo_height()
# Calculate geometry
x = (win.winfo_screenwidth() // 2) - (width // 2)
y = (win.winfo_screenheight() // 2) - (height // 2)
# Set geometry
win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
# Center Window on Screen
center(window)
# Initialize counter
counter = 0
# Define button press function
def press():
# use globally set counter variable
global counter - not the best solution obviously - globals are evil!
# count each button press
counter = counter + 1
# set label
label.config(text=f"Button clicked: {counter} times")
# Place 'Change Label' button on the window
button = tkinter.Button(window, text="Button", command=press)
# Place label at coordinates x=10 and y=10 (top right hand corner)
button.place(x=10, y=40)
# Show new window
window.mainloop()

 

 

Facebook Comments