gui-cpd
gui-cpd
ondon omputing
William Marsh
School of Electronic Engineering and Computer Science
Queen Mary University of London
Outline
• A first program
• Concepts in Graphical User Interface
• Components / widgets and attributes
• Events / actions
• Layout
• Practical examples
• Challenges of GUI programming
• Choosing a GUI library
• Using Object-Oriented programming
First Program – Click the Button
• Code provided but not yet explained
• Use ‘pattern matching’ (i.e. intelligent guessing)
to modify it
Key Concepts
Code for
events Hierarchy of
components
Widgets,
called Attributes
components called
properties
Widgets
• A GUI is made up frame
from widgets
• A widget is created button
• Widget has attributes
• One widget may contain
another:
• Frame contains the button
Create a Widget
• Constructor # Create a main frame with
• Name same as widget # - a title
# - size 200 by 200 pixels
• Hierarchy of widget app = Tk()
app.title("GUI Example 1")
• Optional arguments app.geometry('200x200')
Constructor
Optional
Parent
argument
widget
Attributes set by
Methods to
constructor (note use of
set attributes
keyword arguments)
• Events
• Button, mouse click, key press Name of a
Method
• Action
• Event ‘bound’ to function
Layout the Widget
# Make the button visible at the bottom of the frame
button1.pack(side='bottom')
#!/usr/bin/env python 1
3
• Widgets are classes class Application(tk.Frame):
def __init__(self, master=None):
4
• Default behaviour tk.Frame.__init__(self, master)
self.grid() 5
self.createWidgets()
• GUI programs are
def createWidgets(self):
often organised self.quitButton = tk.Button(self, text='Quit',
command=self.quit) 6
using classes self.quitButton.grid() 7
app = Application() 8
app.master.title('Sample application') 9
app.mainloop() 10