Lecture 8-GUI
Lecture 8-GUI
Python
by Gideon T. Marandu
Ardhi University
Graphical User Interfaces
Concept:
• GUIs allow the user to point at graphical elements and click the
mouse button to activate them.
Graphical User Interfaces
• Interaction with a GUI is done
through dialog boxes – small
windows that display information
and allow the user to perform
actions
Graphical User Interfaces
• GUI Programs Are Event-Driven
• User determines the order in which things happen
• GUI programs respond to the actions of the user, thus they are event
driven.
In Python you can use the tkinter module to create simple GUI
programs.
Using the tkinter Module
• The tkinter package (“Tk interface”) is the standard Python
interface to the Tk GUI toolkit.
import tkinter
def main():
# create the main window widget
main_window = tkinter.Tk()
my_gui = MyGUI()
Display Text with Label Widgets
# This program uses the side='left' argument with.
# the pack method to change the layout of the widgets
import tkinter
Figure: Window displayed by
class MyGUI: Program
def __init__(self):
# Create the main window widget.
self.main_window = tkinter.Tk()
my_gui = MyGUI()
Organizing Widgets with Frames
Concept:
A Frame is a container that can hold other widgets. You can use
Frames to organize the widgets in a window.
Organizing Widgets with Frames
• A Frame is a container.
• It is a widget that can hold other widgets.
• Frames are useful for organizing and arranging groups of widgets in
a window.
Organizing Widgets with Frames
# This program creates labels in two different frames. # Create three Label widgets for the bottom frame
import tkinter self.lbl4 = tkinter.Label(self.bottom_frame, text='Gideon')
class MyGUI: self.lbl5 = tkinter.Label(self.bottom_frame, text='Tumainiel')
def __init__(self): self.lbl6 = tkinter.Label(self.bottom_frame, text='Marandu')
# Create the main window widget.
self.main_window = tkinter.Tk() # Pack the labels that are in the bottom frame
# Use the side='left' argument to arrange them
# Create two frames, one for the top # horizontally from the left of the frame
# window and one for the bottom self.lbl4.pack(side='left')
self.top_frame = tkinter.Frame(self.main_window) self.lbl5.pack(side='left')
self.bottom_frame = tkinter.Frame(self.main_window) self.lbl6.pack(side='left')
# Create three label widget for the top frame # Yes, we have to pack the frames too!
self.lbl1 = tkinter.Label(self.top_frame,text='Gideon') self.top_frame.pack()
self.lbl2 = tkinter.Label(self.top_frame, text='Tumainiel') self.bottom_frame.pack()
self.lbl3 = tkinter.Label(self.top_frame, text='Marandu') # Enter the main loop
self.main_window.mainloop()
# Pack the labels that are in the top frame
# Use the side='top' argument to stack them my_gui = MyGUI()
# one on top of the other
self.lbl1.pack(side='top')
self.lbl2.pack(side='top')
self.lbl3.pack(side='top')
Organizing Widgets with Frames
Figure: Window displayed by Program
lbl1 top_frame
Figure: Arrangement of widgets lbl2
lbl3
bottom_frame
def do_something(self):
class MyGUI:
# Display an info dialog box.
def __init__(self):
messagebox.showinfo('Response', 'Thanks for
# Create the main window widget.
clicking the button')
self.main_window = tkinter.Tk()
my_gui = MyGUI()
Getting Input with the Entry Widget
Concept:
An Entry widget is a rectangular area into which the user can type
input. Use the Entry widget’s get method to retrieve the data that
has been typed into the widget.
Getting Input with the Entry Widget
• An Entry widget is a rectangular area into which the user can type
text.
• These widgets are used to gather input in a GUI program.
• The get method returns a string.
Getting Input with the Entry Widget
""""This program converts distances in kilometers
Program: (kilo_converter.py)
to miles. The result is displayed in an info
# Pack the top frame's widgets
dialog box"""
self.prompt_label.pack(side='left')
self.kilo_entry.pack(side='left')
import tkinter
from tkinter import messagebox
# Create the button widgets for the bottom frame.
self.calc_button = tkinter.Button(self.bottom_frame,
class KiloConverterGUI:
text='Convert',
def __init__(self):
command=self.convert)
# Create the main window widget.
self.quit_button = tkinter.Button(self.bottom_frame, text='Quit',
self.main_window = tkinter.Tk()
command=self.main_window.quit)
# Pack the buttons
# Create two frames, one for the top
self.calc_button.pack(side='left')
self.top_frame = tkinter.Frame(self.main_window)
self.quit_button.pack(side='left')
self.bottom_frame = tkinter.Frame(self.main_window)
import tkinter
# Create the widgets for the middle frame.
self.descr_label = tkinter.Label(self.mid_frame,
class KiloConverterGUI:
text='Converted to miles')
def __init__(self):
# Create the main window widget.
# We need a StringVar object to associate with
self.main_window = tkinter.Tk()
# an output label. Use the object's set method
# to store a string of blank characters.
# Create two frames, one for the top
self.value = tkinter.StringVar()
self.top_frame = tkinter.Frame(self.main_window)
self.mid_frame = tkinter.Frame(self.main_window)
# Create a label and associate it with the
self.bottom_frame = tkinter.Frame(self.main_window)
# StringVar object. Any value stored in the
# StringVar object will automatically be displayed
# Create the widgets for the top frame.
# in the label
self.prompt_label = tkinter.Label(self.top_frame,
self.miles_label = tkinter.Label(self.mid_frame,
text='Enter a distance in kilometers:')
textvariable=self.value)
self.kilo_entry = tkinter.Entry(self.top_frame,
width=10)
Using Labels as Output Fields
Program (cont.): (kilo_converter2.py) # Enter the tkinter main loop
self.main_window.mainloop()
Radio buttons normally appear in groups of two or more and allow the
user to select one of several possible options. Check buttons, which
may appear alone or in groups, allow the user to make yes/no or
on/off selections.
Radio Buttons
• Radio buttons are used when the user needs to select one choice from
several options
self.radio_var.set(1) command=self.main_window.quit)
Program (cont.): (radiobutton_demo.py)
Radio Buttons
# Pack the Buttons
self.ok_button.pack(side='left')
self.quit_button.pack(side='left')
QUESTIONS
?