Gui Practical Sheet
Gui Practical Sheet
Page 1 of 10
Introduction to GUI Programming in Python July 17
Also experiment with ‘usual’ windows operation such as resizing the window,
minimising it and closing it.
Exercise 1.2: Modify the Program
Although it has not been explained yet, see if you can figure out how to make the
following modifications:
• Change the title
• Change the text in the button
• Change the text printed when the button is pressed
• Change the size (geometry) of the rectangular frame
• Move the button to the top of the frame
Page 2 of 10
Introduction to GUI Programming in Python July 17
• When the button is pressed the text changes, toggling between two messages.
Page 3 of 10
Introduction to GUI Programming in Python July 17
The desired behaviour is shown below. The four labels are positioned at the corners and
the labels fill the space when the window is resized.
Page 4 of 10
Introduction to GUI Programming in Python July 17
Page 5 of 10
Introduction to GUI Programming in Python July 17
It does not do much yet! Change it to a simple application that can read a file, convert it
to upper case and save it again. The commands are:
Menu Command Description
Open Open a new file
File Save Save the existing file
Quit Quit the application
Edit Convert to upper Convert the file contents to upper case
Help Useful help
About
About Information about the application
Page 6 of 10
Introduction to GUI Programming in Python July 17
Page 7 of 10
Introduction to GUI Programming in Python July 17
8 Additional Notes
8.1 Layout
When a widget is created it needs to be positioned inside its parent. The positioning also
needs to consider the possibility that the window may be resized. Layout is the
responsibility of a layout manager: Tkinter offers a choice of layout managers.
These notes consider the ‘pack’ layout manager. It is simplest to get started with and
also behaves ok for resizing. Other layout managers are ‘place’ and ‘grid’; the latter is
often recommended for more complex layouts.
The following two example program fragments illustrate the principles:
#
# Create three labels of given width
#
bA = Label(app, text="A", width=12, bg='red')
bB = Label(app, text="B", width=12, bg='yellow')
bC = Label(app, text="C", width=12, bg='blue')
# Pack horizontally
# -----------------
# Horizontal packing with
# side = "left"
# side = "right"
bA.pack(side='left')
bB.pack(side='left')
bC.pack(side='left')
gives the display:
Page 8 of 10
Introduction to GUI Programming in Python July 17
Methods
.geometry(newGeometry=None)
Set the window geometry. For example:
• 200x400
• 200x400+10+20
• 200x400-10-20
If the argument is omitted, the current geometry string is returned.
.title(text=None)
Set the window title. If the argument is omitted, returns the current title.
Page 9 of 10
CAS London CPD Day July 17
On messages with a choice, the default choice is the first one. Use the default option
default = C
to change this where C = CANCEL, IGNORE, OK, NO, RETRY, or YES
Each of the “ask...” pop-up functions returns a value that depends on which button the
user pushed to remove the pop-up.
• askokcancel, askretrycancel, and askyesno all return a bool value: True for “OK”
or “Yes” choices, False for “No” or “Cancel” choices.
• askquestion returns u'yes' for “Yes”, or u'no' for “No”.
Page 10 of 10