Year 1 Computer Programming - Lecture 9
Year 1 Computer Programming - Lecture 9
GUI Programming
(Based on Gaddis, T., Starting out with Python 3e, Pearson Education)
User Interface: the part of the computer with which the user
interacts
Command line interface / text based interface: displays a
prompt and the user types a command that is then executed
Label
Image
Checkbutton Button
Entry
Widgets Description
Button A button that can cause an action to occur when it is clicked.
Canvas A rectangular area that can be used to display graphics.
Checkbutton A button that may be in either the “on” or “off” position.
Entry An area in which the user may type a single line of input from the keyboard.
Frame A container that can hold other widgets.
Label An area that displays one line of text or an image.
Listbox A list from which the user may select an item
Menu A list of menu choices that are displayed when the user clicks a Menubutton
widget.
Menubutton A menu that is displayed on the screen and may be clicked by the user
Message Displays multiple lines of text.
Radiobutton A widget that can be either selected or deselected. Radiobutton widgets usually
appear in groups and allow the user to select one of several options.
Scale A widget that allows the user to select a value by moving a slider along a track.
Scrollbar Can be used with some other types of widgets to provide scrolling ability.
Text A widget that allows the user to enter multiple lines of text input.
Toplevel A container, like a Frame, but displayed in its own window.
import tkinter
class Label_GUI:
def __init__(self):
#create main window
self.mw = tkinter.Tk()
self.label1 = tkinter.Label(self.mw, text = "Hello World")
self.label1.pack() mainloop is an infinite loop that listens
tkinter.mainloop() to user interactions (mouse clicks, key
presses, etc) and requests from the
gui = Label_GUI() OS/window manager to draw or
redraw a widget
import tkinter
class Label_GUI:
def __init__(self):
#create main window
self.mw = tkinter.Tk()
import tkinter
class Button_GUI:
def __init__(self):
#create main window
self.mw = tkinter.Tk()
def function_1(self):
#do something when button 1 is clicked
print("Button 1 is clicked")
def function_2(self):
#do something when button 2 is clicked
print("Button 2 is clicked")
Button_GUI()
Entry widget: rectangular area that the user can type text into
– Used to gather input in a GUI program
– Typically followed by a button for submitting the data
- The button’s callback function retrieves the data from the Entry
widgets and processes it
– Format
entry1 = tkinter.Entry(container, width = ..)
– Entry widget’s get method: used to retrieve the data from an Entry
widget
- Returns a string
e1 = tkinter.Entry(self.mw)
e2 = tkinter.Entry(self.mw)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
0 1 2
bt1 = tkinter.Button(self.mw, text = "Enter")
bt1.grid(row=2, column=2)
0 Label Entry
tkinter.mainloop()
1 Label Entry