Python GUI programming with Tkinter

Python GUI programming with Tkinter

Learn how to create a simple graphical user interface in Python using the pre-installed Tkinter package


Tkinter is a package used to create simple graphical user interfaces (GUI). With Tkinter we can spawn a window wherein we can add our own widgets such as frames, labels, buttons, inputs and more, it also allows us to handle events such as button clicks and mouse movements with our own functions, as well as allowing us to dynamically interact with the GUI through our program.


Creating a basic GUI with Tkinter


To make use of the Tkinter package we must first import it, the package name is either "Tkinter" if you are using Python version 2.x or "tkinter" without the capital letter if you are using version 3.x. As an example let's create a simple Tkinter window with a single label.


from tkinter import *   
   
window = Tk()   
   
testLabel = Label(window, text="test content")   
testLabel.pack()   
   
window.mainloop()   

In the above example we first create a new window object using the Tk class, this is what will be used to house all other components of the GUI. Next we create a new label and assign it to the window object, we then assign a text value "test content" which is what will appear on the label. Next, using the pack() function we will add the label to its assigned container which is our window object.


Now that our window is ready we can use the mainloop() function to display the window, this function must come last as it starts a loop that only ends when the window is closed, the purpose this loop serves is to listen for and handle events within the window.


The above example will display the following GUI:



As another example let's create a slightly more complicated GUI with buttons, text input and images


from tkinter import *   
   
window = Tk()   
   
logo = PhotoImage(file="logo.png")   
testLabel = Label(window, image=logo)   
testLabel.pack()   
   
testEntry = Entry(window)   
testEntry.pack()   
   
def buttonClick():   
    print( testEntry.get() )   
       
testButton = Button(window, text="click me", command=buttonClick)   
testButton.pack()   
   
window.mainloop()

In this example we create a new label that we assign an image to using the image argument for Label objects, this will display the image instead of text content. Next we create a new Entry widget which allows users to enter their own text, later we can then fetch that input within our code and make use of it as needed. Now we create a new button along with a function that will be called as the button is clicked, this function will print out the value of the Entry widget which we fetch using the get() function, to assign the function to the button, we assign the function name (without arguments) to the command argument of the Button constructor.


This code will produce the following window:



Here, as a test, we can enter text such as "test input" into the Entry widget, then we can press the button at which point the buttonClick() function will activate and print "test input" to the console.


Events are not limited to buttons clicks, for a much wider range of event handling have a look at our tutorial on

Tkinter event handling

which covers the bind() function.



Christopher Thornton@Instructobit 7 years ago
or