How to Start a Tkinter Widget Application
Create a Tkinter Widgets app: The following statements are required for writing any tkinter application.
from tkinter import *
root = Tk()
root.mainloop()
from tkinter import * # 1
root = Tk() # 2
root.mainloop() # 3
Each of the three numbered code lines is described below to create tkinter widgets app.
From tkinter import *
- The above statement imports tkinter so it can be used in our application.
root = Tk()
root = Tk()
statement creates a top-level window.- Generally, the top-level window is called ‘root’ and is the highest-level GUI component in any Tkinter application.
- At this step, a window appears on your desktop. The window includes the maximize, minimize, and close icons. See the image below.
root.mainloop()
.- Mainloop is a method of the ‘root’ object. This statement executes the ‘mainloop’ monitoring for any events. If any events occur, they are handled by the event handler. The “mainloop” runs continuously and forever until the user closes the “root” window.
Basic Steps For Creating Tkinter Widgets App
The following are basic common steps for creating any tkinter widgets application. You can use these steps as a template for creating any GUI. Refer to the Button program for the steps below.
- Import tkinter module
- Create a “top-level” window. The statement
root = Tk()
creates the top-level window root”. This top-level window (called “root”) is the highest-level GUI component in a Tkinter application. - Create an instance of the Frame class.
- The statement frame1 = Frame(root) creates an instance of the Frame class.
- This statement names this instance as “frame1”. You can use any name of your choosing.
- The instance “frame1” is a child of the parent “root”.
- Run pack method on ‘frame1’ instance. See program statement frame1.pack()
- Create an instance of the widget class and associate it with the parent “frame1”.
- In the below program, “btn” is an instance of the Button class. “btn” is now a child of parent “frame1”.
- Set the widget’s attributes (such as color, and size) corresponding to the widget
- Pack the widget to size itself.
- Run
root.mainloop()
method.- The “root” object has a method called “mainloop”. In the creation of the application,
root.mainloop()
statement operates the “mainloop” method on the “root” object. The “mainloop” method runs continuously (unless stopped by the user) and waits for events (such as the user clicking the mouse) to occur in “root”. (We will discuss events in a later section). - The statement
root.mainloop()
is a method that is executed if we want to run our application. -
root.mainloop()
will exit if the user exits the program by closing the window.
- The “root” object has a method called “mainloop”. In the creation of the application,
In the rest of this chapter, the program examples will clarify these steps in creating a Tkinter Widgets application.
Tkinter Widgets Classes
Tkinter has several built-in widget classes. We will write programs using only a few of them, namely:
- Label
- Button
- Frame
- Entry
- Canvas
There are many more built-in Tkinter widgets classes. The above widget classes are the only ones that we will present on this website. A complete list of widget classes is described in the link below:
<a href=”https://www.scripps.edu/sanner/python/inputform/tkinterWidgets.html“>
Let us start with learning the Tkinter widgets. The first one we will describe is the Label class.
Label Widgets
The Label is a tkinter class. The Label is one of the tkinter widgets. The user can view the label widget, but cannot interact with it, that is, it is a static widget. The Label widget can display either text or images.
The Label widget lets us specify the fonts used to display the text. The fonts are specified by selecting the attribute “font” using an option. The options select the font size (points), the font face (normal/bold/italics), etc. See the example program below.
The following program creates a simple Label GUI that displays text. In the next section, we will create a Label widget that displays an image.
PROGRAM EXAMPLE: TKINTER LABEL WIDGET
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> from tkinter import * # 1)
>>> root = Tk() # 2)
>>> lblx = Label(root, text = 'Label Example', fg = 'lightgreen', bg = 'red', font = 'verdana 16 bold') # 3)
>>> lblx.pack() # 4)
>>> root.mainloop() # 5)
Detailed Line-by-line Program Explanation:
The line numbers in the explanation below refer to the code line numbers in the program above.
from tkinter import *
- As described in the section “How to Start a Tkinter Application” above, the first step in any tkinter widgets app is to import the Tkinter module.
- This statement imports the tkinter module along with
Tk()
toolkit. This module contains all classes, functions, etc. needed to work with the Tk toolkit.
root = Tk()
- The next step is to initialize Tkinter. Initialization is done by creating a Tk root widget.
- root = Tk() statement initializes the tkinter module and creates a Tk root widget. The root widget is simply a window with a title bar, showing the close icon (x), minimize icon (-), and maximize icons.
- When you execute the root = Tk() statement, you will see a window with a title bar on your desktop with the maximize, minimize, and close icons. See the figure above in the “How to Start a Tkinter Widget Application” section.
- The root widget must be created before any other widgets are created. “root” is the highest-level widget in the application. All other widgets are children of the root widget.
lblx =
lblx = Label(root, text = ‘Label Example’, fg = ‘lightgreen’, bg = ‘red’, font = ‘verdana 16 bold’)- This step creates an instance of the Label class and names it ‘lblx”. (You can choose any name you like.)
- The parameters are specified within the parenthesis of the statement.
- The first parameter must be the parent widget. The parent widget “root” was created in step 2).
- The statement in this step
- The instance “lblx” created in this step is a child of the parent “root”.
- In this step, we also set the attributes that define how we want to make the label widget look.
- The widget look is defined by using keyword parameters and attributes. See the section “Keywords and Font Configuration Attributes” below.
lblx.pack()
- This step runs the
pack()
method on the object “lblx”. - The
pack()
method stretches or shrinks the label widget to fit the text. - Additionally, the
pack()
method makes the widget visible. Before this step, you will not see the widget; you will just see the window you created in step 2). After you enterpack()
on your keyboard, you will notice the window that you created in step 2 shrink to the size of the text you entered in step 3).
- This step runs the
root.mainloop()
- Finally, in step 5), we run the method “mainloop” on object “root” to run the application, which we just wrote.
- The method
mainloop()
continues to run the application, waiting for an event to occur and to process the event as long as the window is not closed. This event loop handles all events from the user (keyboard key press, mouse clicks, etc.). Note: In this example, we do not have any events; we will write programs that handle events in the following sections.mainloop()
method runs continuously until you (the user) close the window
Keywords and Font Configuration Attributes for Step 3):
- text: This text shows on the Label (in this case “Label Example”.
- foreground (fg): This attribute selects the foreground color of the widget (light green in this case)
- background (bg): This attribute selects background color of the widget (red in this case)
- font: specifies the font type to use (Verdana in this case); font-size (16 points in this case; font style (bold in this case)
If you run this program, you will see the following window on your desktop. As mentioned earlier, you cannot interact with the label widget. In a later section, when we create Button tkinter widget programs, you will be able to interact with the button tkinter widgets by clicking on it.
We will create many interesting UI using other Tkinter widgets in the next few sections.
Label with Image Instead of Text
In the previous section, we created Label tkinter widgets that contained only text. In this section, we will create Label tkinter widgets with an image.
We will use the Python.org logo as our label image. First, copy the Python logo image from www.python.org and store it in the directory C:\Program Files (x86)\Python38-32 on your computer.
In this program, we named the Python logo image as “PythonOrgLogo.png”. You can use any name you like. Below is the label widget program that displays the Python.org logo.
PROGRAM EXAMPLE: LABEL WIDGET WITH IMAGE
>>> from tkinter import * # 1)
>>> root = Tk() # 2)
>>>
>>> PythonLogo = PhotoImage(file="PythonOrgLogo.png") # 3)
>>> labelX = Label(root, image = PythonLogo) # 4)
>>> labelX.pack() # 5)
>>> root.mainloop() # 6)
If you run this program, you will see the following image on your desktop.
Detailed line-by-line Explanation of the Program:
The line numbers in the explanation below refer to the code line numbers in the program above.
- Step 1) of this program is the same as for Label with text program
- Step 2) of this program is also the same as in the case of the label with text program.
PythonLogo = PhotoImage
(file=”PythonOrgLogo.png”).- In this step, we tell the Python interpreter the name of the image file we stored in the above directory. The name of the file is “PythonOrgLogo.png”.
- For this step, we create an object by using the PhotoImage() method.
- Note that the PhotoImage class can read only GIF and PGM/PPM images from files.
labelX = Label
(root, image = PythonLogo). In this step, we create an instance of the Label class and name it “labelX”. This step tells the Python interpreter to use the image file defined in step 3 called “PythonLogo”.labelx.pack()
Run thepack()
method on the Label object. “labelx”.labelx.pack()
method will enlarge or shrink the widget to the size of the image.root.mainloop()
- Run the method “mainloop” on object “root” to run the application, we just wrote.
- The
mainloop()
method continues to run the application for ever until the user closes the window.