Professional Documents
Culture Documents
Module01 Wk03a GuiApplication
Module01 Wk03a GuiApplication
Module01 Wk03a GuiApplication
GUI Applications
Networking for Software Developer
Narendra Pershad
Agenda
Handling event
CLI vs Gui
CLI
Command Line Interface will be the most common way of deploying python code in this course
It also automatically generates help and usage messages and issues errors
PyQt.
Bindings for the Qt cross-platform C++ framework
Both this and the toolkit below does not use platform widgets, so they are lighter and faster
Tkinter
The default gui toolkit for standard installation
Very stable
tk vs ttk
ttk offers more sophisticated theme styling
Most of the widgets are comparable
To get the best of both worlds use the following style of import statements
This will give you access to the themed versions of the widgets if available in
the ttk library otherwise fall back to the original tk library
Button
Button(
)
Label
Can be used to display either an image or some text
Label(
)
Entry
Similar to a single lined textbox
You may use the get() and insert() methods to interface with this widget
Entry(
)
Frame
Can serve as a container for other widgets
Is used to organised other widgets
Its cousin LabelFrame is able to show text directly
Frame(
parent, # the host container
cursor, # the pointer style to show when over this widget
style, # the style
)
LabelFrame(
parent, # the host container
text=«text to display»
)
Text
Multi-lined formatted text
It can also handle images
get(), insert() and delete() methods facilitate text editing
Text(
height=«number of text lines», # the string var that is bound to this widget
)
Radiobutton
Implements a multiple-choice button
You may elect to define a call back function
def sel():
text = f'You have selected {residency.get()}'
residency = StringVar()
Radiobutton(parent, text='Domestic',
variable=residency, value='dom', command=sel)
Radiobutton(parent, text='International',
variable=residency, value='intl', command=sel)
Combobox
Implements a drop-down list
program = StringVar()
comp100 = StringVar()
comp213 = StringVar()
comp120 = StringVar()
Options
column=«column to put this widget, default=0»
columnspan=«number of column to occupy, default=1»
row=«row to put this widget, default=0»
rowspan=«number of rows to occupy, default=1»
sticky=«what to do if cell is larger than widget, E, W, N, S, NE, NW, SE, SW default=0»
The following will distribute 2/5 of extra space to column 0 and 3/5 to column 1
w.columnconfigure(0, weight=2)
w.columnconfigure(1, weight=3)
Placing widget in container – pack()
pack()
Simple for simple layout, but may become complex for slightly complex
layout
Options:
expand=«True, False(default)»
fill=«X, Y, BOTH, NONE(default)»
side=«LEFT, RIGHT, BOTTOM, TOP(default)»
Placing widget in container – place()
place()
Provides the best placement capability
Options:
x, y=«distance from left or top of parent container»
width, height=«size in pixel of widget»
relx, rely=«relative distance from left or top of parent container»
relwidth, relheight=«relative size in pixel of widget»
fill=«X, Y, BOTH, NONE(default)»
side=«LEFT, RIGHT, BOTTOM, TOP(default)»
Using internal bitmaps
A container can a Tk window, frame, canvas or panel
To put the widget onto its container, use the following:
e.g.
Button(parent, text='Error',
relief=RAISED, bitmap='error')
Button(parent, text='Error',
relief=RAISED, bitmap='error’)
dir = filedialog.askdirectory()
messagebox.showinfo('Message title', 'Message content')
messagebox.showwarning('Message title', 'Message content') #shows warning message
messagebox.showerror('Message title', 'Message content') #shows error message
height=240) #height
Label Entry
Label
Radiobutton
Radiobutton
Label Combobox
Label Checkbutton
Checkbutton
Checkbutton
column=1, #column=1
row=0, #row=0
panel = ttk.Frame(frame) #this will be the container for the widget below
panel.grid(column=1, row=2, sticky=(W, E))
panel['borderwidth'] = 3
panel['relief'] = 'ridge'
residency = StringVar()
panel = ttk.Frame(frame)
panel.grid( column=1, row=4, columnspan=4, sticky=(W, E)
panel['borderwidth'] = 3
panel['relief'] = 'ridge'
comp100 = StringVar()
comp213 = StringVar()
comp120 = StringVar()
#row 4 continued
check = ttk.Checkbutton( panel,
text='Programming I',
variable=comp100,
# offvalue='imperial',
onvalue='comp100')
check.grid(column=0, row=0, sticky=(W))
ttk.Button(frame, text='Ok',
command=read_form).grid( #the function that will be called
column=1, row=5, sticky=(W, E))
ttk.Button(frame, text='Exit',
command=root.quit).grid( #the delegate that will be called
column=2, row=5, sticky=(W, E))
def read_form(*args):
messagebox.showinfo( #from tkinter import messagebox
title='Form Information',
message=f'Username: {username.get()} \nResidency: {residency.get()}\nCourses:
{comp100.get()} {comp120.get()} {comp213.get()}\nProgram: {program.get()}')
Summary