Graphical User Interface
Graphical User Interface
Graphical User Interface (GUI) is nothing but a desktop application which helps you to interact with the
computers. They perform different tasks in the desktops, laptops and other electronic devices.
GUI apps like Text-Editors create, read, update and delete different types of files.
Apps like Sudoku, Chess and Solitaire are games which you can play.
GUI apps like Google Chrome, Firefox and Microsoft Edge browse through the Internet.
There are many tool kit options available for developing graphical user interface
(GUI) . Most popular are given below:
Kivy
Python QT
wxPython
Tkinter
What Is Tkinter?
Tkinter is actually an inbuilt Python module used to create simple GUI apps. It is the most commonly
used module for GUI apps in the Python.
Fundamentals Of Tkinter
Consider the following diagram, it shows how an application actually executes in Tkinter:
Tkinter Widgets
Widget is a graphical user interface controls to interact between user and computer. Tkinter gives different types of
controls such as buttons, labels and text boxes used in a GUI application. Tkinter widgets are described in the following
table.
Label Widget:
Labels are used to create texts and images and all of that but it is important to note that it has to be a
single line definition only.
Output:
import Tkinter as tk
import tkMessageBox as bx
tp=[Link]( )
def exbutton( ):
[Link]( )
[Link]( )
Figure 14.3. Message “Hello World” will appear when "Press this button" is clicked
The output of the above program is shown in fig 14.3. When the button called
‘Press this button’ is clicked, the message box ‘Hello World’ will appear.
Parameters:
selectborderwidth The width of the border to use around selected text. The
default is one pixel.
selectforeground The foreground (text) color of selected text.
show Normally, the characters that the user types appear in the
entry. To make a .password. entry that echoes each
character as an asterisk, set show="*".
state The default is state=NORMAL, but you can use
state=DISABLED to gray out the control and make it
unresponsive. If the cursor is currently over the
checkbutton, the state is ACTIVE.
Width The default width of a checkbutton is determined by the
size of the displayed image or text. You can set this option
to a number of characters and the checkbutton will always
have room for that many characters.
Example 14.3. This program describes how to use a Tkinter entry in a program.
2 combo = Combobox(window)
4 [Link](3)
[Link](column=0, row=0)
5
So check out that there are no other parameters for the combobox definition apart from the window.
And in the next line, we have defined certain values such numbers ranging from 1 to 5 and next, we
have text. 1 to 5 were numeric inputs but we can have a textual input too.
It is defined using double quotes and we later will set the selected input. Followed by that, we have
the grid function to place the widget on the window.
So we have the drop-down menu and it displays all that we’ve defined in the code. Here is the output
for the code:
The next widget we will check on this Tkinter tutorial blog is the Checkbutton widget.
Checkbutton Widget:
The checkbutton is widely used in almost all the sites.
So basically we use the checkbutton class to create the widget.
Code snippet:
1 chk_state = BooleanVar()
2 chk_state.set (True)
4 [Link](column=0, row=0)
But this is not a standard python variable, correct? Well nothing to worry, this is a Tkinter variable.
By default, we keep the set state to be true which means that the button is checked already. And
next, we are passing chk_state to the checkbutton class to set the check state for us.
Output:
Check out the above output. So we have a really simple checkbutton widget with a certain text.
The next widget we will check on this Tkinter tutorial blog is the radio button widget.
4 [Link](column=0, row=0)
5 [Link](column=1, row=0)
[Link](column=2, row=0)
6
Here, we have the value parameters to be different. 1,2 and 3. However, if they are same, it will lead
to conflict and there will be an error. So it is to be noted that a unique value is used to address the
radio buttons.
The value should be unique but the textual data can be the same, however. Here we have considered
Python, Java, and Scala. It can be whatever you want it to be based on the requirements.
Similarly, the grid function used to place the widget on the window.
Output:
From the above output, do note that unlike a checkbutton where you can try selecting multiple, here
in case of radio button you can only select one at a time.
The next widget we will check on this Tkinter tutorial blog is the scrolled text widget.
Code:
You can set the scrolled text content by using the insert method. The syntax is pretty simple. We
need to use [Link] with the message as a parameter.
Output:
The next widget we will check on this Tkinter tutorial blog is the message box widget.
Code:
See, this is where things get interesting. Look at the snippet below:
1 def clicked():
2 [Link]('Message title', 'Message content')
3
4 btn = Button(window,text=‘ENTER', command=clicked)
Here we have made use of two of the widgets we learnt. We are using a button click to show a
message box for us.
Last but not least, we will check out the Spinbox widget on this Tkinter tutorial.
SpinBox Widget:
Spinbox is a popular widget as well. There are two tabs, the up and down scroll tabs present. This is
how it differs from the scroll down widget. Here the static number will change over a certain range of
values.
Code:
Width is basically to set the size of the widget to 5 character spaces. But since are doing 0 to 100, 3
is sufficient for us but I have gone ahead and put 5 just so it looks well placed.
You can put whatever you want here and it’s valid but make sure it is more than what the range can
offer.
Output:
Next up on this Tkinter tutorial blog, we need to check out geometry management.
Geometry Management
All widgets in the Tkinter will have some geometry measurements. These measurements give you to
organize the widgets and their parent frames, windows and so on.
pack():- It organizes the widgets in the block, which mean it occupies the entire available
width. It’s a standard method to show the widgets in the window.
grid():- It organizes the widgets in table-like structure.
place():- It places the widgets at a specific position you want.
We already looked at the grid in almost all of the previous codes. If you have any doubts, head to the
comment section and leave a comment, let’s interact there.
Next up on this Tkinter tutorial blog, we need to check out how we can organize layouts and widgets.
Organizing Layouts And Widgets
To arrange the layout in the window, we will use Frame, class. Let’s create a simple program to see
how the Frameworks.
Steps:-
Frame creates the divisions in the window. You can align the frames as you like
with side parameter of pack() method.
Button creates a button in the window. It takes several parameters like text (Value of the
Button), fg (Color of the text), bg (Background color)
Note – The parameter of any widget method must be where to place the widget.
1
import tkinter
2 window = [Link]()
3 [Link]("GUI")
4 # creating 2 frames TOP and BOTTOM
5 top_frame = [Link](window).pack()
bottom_frame = [Link](window).pack(side = "bottom")
6 # now, create some widgets in the top_frame and bottom_frame
7 btn1 = [Link](top_frame, text = "Button1", fg = "red").pack()# 'fg - foreground' is
8 btn2 = [Link](top_frame, text = "Button2", fg = "green").pack()# 'text' is used to
9 btn3 = [Link](bottom_frame, text = "Button2", fg = "purple").pack(side = "left")#
10 widgets
btn4 = [Link](bottom_frame, text = "Button2", fg = "orange").pack(s
11 [Link]()
12
Above code produces the following window, if you didn’t change the above code.
1 import tkinter
window = [Link]()
2
[Link]("GUI")
3 # creating 2 text labels and input labels
4 [Link](window, text = "Username").grid(row = 0) # this is place
5 # 'Entry' is used to display the input-field
6 [Link](window).grid(row = 0, column = 1) # this is placed in
[Link](window, text = "Password").grid(row = 1) # this is place
7 [Link](window).grid(row = 1, column = 1) # this is placed in
8 # 'Checkbutton' is used to create the check buttons
9 [Link](window, text = "Keep Me Logged In").grid(columnspan = 2) # 'columnspan'
10 columns
11 # you can also use 'rowspan' in the similar manner
12 [Link]()
13
You will get the following output:
Next up on this Tkinter tutorial blog, we need to check out a concept called binding functions.
Binding Functions
Calling functions whenever an event occurs refers to a binding function.
In the below example, when you click the button, it calls a function called say_hi.
1
import tkinter
2 window = [Link]()
3 [Link]("GUI")
4 # creating a function called say_hi()
5 def say_hi():
6
7 [Link](window, text = "Hi").pack()
[Link](window, text = "Click Me!", command = say_hi).pack() # 'command' is execute
8 # in this above case we're calling the function 'say_hi'.
9 [Link]()
10
The above program will produce the following results:
Another way to bind functions is by using events. Events are something like mousemove,
mouseover, clicking, and scrolling.
The following program also produces the same output as the above one:
1
2 import tkinter
window = [Link]()
3 [Link]("GUI")
4 # creating a function with an arguments 'event'
5 def say_hi(event): # you can rename 'event' to anything you want
6
7 [Link](window, text = "Hi").pack()
btn = [Link](window, text = "Click Me!")
8 [Link]("Button-1", say_hi) # 'bind' takes 2 parameters 1st is 'event' 2nd is 'func
9 [Link]()
10 [Link]()
11
‘<Button-1>‘ parameter of bind method is the left clicking event, i.e., when you click the left
button the bind method call the function say_hi
o <Button-1> for left click
o <Button-2> for middle click
o <Button-3> for right click
Here, we are binding the left click event to a button. You can bind it to any other widget you
want.
You will have different parameters for different events
Clicking events are of 3 different types namely leftClick, middleClick, and rightClick.
Now, you will learn how to call a particular function based on the event that occurs.
Run the following program and click the left, middle, right buttons to calls a specific function.
That function will create a new label with the mentioned text.
1
2 import tkinter
3 window = [Link]()
4 [Link]("GUI")
#creating 3 different functions for 3 events
5 def left_click(event):
6
7 [Link](window, text = "Left Click!").pack()
8 def middle_click(event):
9
10 [Link](window, text = "Middle Click!").pack()
def right_click(event):
11
12 [Link](window, text = "Right Click!").pack()
13 [Link]("Button-1", left_click)
14 [Link]("Button-2", middle_click)
15 [Link]("Button-3", right_click)
16 [Link]()
17
If you run the above program, you will see a blank window. Now, click the left, middle and
right button to call respective functions.
You get the something similar results to the following:
Next up on this Tkinter tutorial blog, we need to check out how we can add images to our window.
1
import tkinter
2 window = [Link]()
3 [Link]("GUI")
4 # taking image from the directory and storing the source in a variable
5 icon = [Link](file = "images/[Link]")
6 # displaying the picture using a 'Label' by passing the 'picture' variriable to 'imag
label = [Link](window, image = icon)
7 [Link]()
8 [Link]()
9
You can see the icon in the GUI:
So next up on this Tkinter Tutorial blog, we’re going to create a simple Calculator GUI with all the
stuff that we’ve learned till now.
Use – Case : Calculator App Using Tkinter
Every GUI apps include two steps:
[Link]()