Tkinter is the standard GUI library for Python and the good news its already available so no extra packages to install here.
Tkinter provides an interface to the Tk GUI toolkit.
Creating a GUI application using Tkinter is extremely straightforward.
First of all you just import the Tkinter module.
Then you create the main window of your GUI.
You then add the widgets your require to the GUI application. We will show you a list of available widgets later on in this article
Enter the main event loop to take action against each event, such as a user clicking on a button or interacting with a widget like a checkbox, slider, scrollbar etc.
In previous versions of Python you would do the import like this
import Tkinter
I am using Python 3.8.2 so the expected import is all in lowercase now like
import tkinter
This is just something to be aware of depending on which version of Python you have installed or plan to installed
Lets look at the most basic example which simply displays an empty window
#!/usr/bin/python import tkinter top = tkinter.Tk() top.mainloop()
Save this as tkinter1.py, open a command prompt and run it like python tkinter1.py. This is what you should see. I adjusted the window below the command prompt.
Tkinter Widgets
Now a blank window is all good and well but as you know most of the functionality of an application usually lies in the controls that can be added such as buttons, text boxes, check boxes and labels to display text. These are known as widgets in tkinter
Here are a list of the widgets you can use
Widget | Description |
Button | This is used to display buttons. |
Canvas | This is used to draw shapes |
Checkbutton | This is used to display a number of options as checkboxes. it is possible to select a number of options in the checkbox |
Entry | This is used to display a single line text box for accepting user input |
Frame | This is used as a container for other widgets |
Label | This is used to provide a single line caption for other widgets. |
Listbox | This is used to provide a list of options |
Menubutton | This is used to display menus. |
Menu | This is used to provide various commands. These commands are contained inside the Menubutton widget. |
Message | This is used to display multiline text fields |
Radiobutton | This is used to display a number of options as radio buttons. You can only select one option. |
Scale | This is used to provide a slider. |
Scrollbar | This is used to add scrolling capabilities to supported widgets. |
Text | This is used to display text in multiple lines. |
Toplevel | This is used to provide a separate window container. |
Spinbox | This is used to select from a fixed number of values. |
PanedWindow | This is a container that may contain any number of panes, arranged horizontally or vertically. |
LabelFrame | This is a container. Its mainly used as a spacer or container for window layouts. |
tkMessageBox | This is used to display a message box. |
We will look at some of these widgets in future articles
Here is a taster for another simple GUI example
from tkinter import * class Application(Frame): def hello(self): print ("hello world, I'm tkinter") def createGUI(self): #a button with red text on a black background #that says EXIT that quits the app self.QUIT = Button(self) self.QUIT["text"] = "EXIT" self.QUIT["fg"] = "yellow" self.QUIT["bg"] = "black" self.QUIT["command"] = self.quit self.QUIT.pack({"side": "left"}) #a button that will say the contents of hello on #the comamnd line self.hi_there = Button(self) self.hi_there["text"] = "Hello", self.hi_there["command"] = self.hello self.hi_there.pack({"side": "left"}) def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createGUI() root = Tk() app = Application(master=root) app.mainloop() root.destroy()
Save that as tkinter2.py , open a command prompt and run using python tkinter.py, this is what you should see