computing science 1p lecture 18: friday 2 nd march simon gay department of computing science...
TRANSCRIPT
Computing Science 1P
Lecture 18: Friday 2nd March
Simon GayDepartment of Computing Science
University of Glasgow
2006/07
2006/07 Computing Science 1P Lecture 18 - Simon Gay 2
Announcement
On Wednesday next week three non-standard things will happen.
The first is that the class will be taken by Peter Saffrey, as Ihave to be away. Peter is one of our most experienced tutors,and some of you know him from the accelerator course. He willcover some useful examples, and you should also find it helpful to hear from someone different for a change.
2006/07 Computing Science 1P Lecture 18 - Simon Gay 3
Announcement
On Wednesday next week three non-standard things will happen.
The second is that Steve Draper from the Psychologydepartment will be running an evaluation of CS1P, after thelecture. This is to help us with revisions to the new version ofthe module. If you volunteer to help with the evaluation, it willbe very helpful to us (and you will get a free lunch). Steve willarrive at the end of this lecture to say more about it.
2006/07 Computing Science 1P Lecture 18 - Simon Gay 4
Announcement
On Wednesday next week three non-standard things will happen.
The third is that it will probably not be possible to hand out the Unit 16 exercise sheet. You will get it on the Friday instead.Sorry.
2006/07 Computing Science 1P Lecture 18 - Simon Gay 5
Another GUI example
Recall one of the first semester's exercises: working out whether or not a given text is a palindrome (ignoring spacesand punctuation).
Let's put a graphical user interface on it.
First we'll reconstruct the original solution.
2006/07 Computing Science 1P Lecture 18 - Simon Gay 6
Checking for Palindromes
def palindrome(s): # s is a string t = string.lower(s) # lower case version u = "" for x in t: if x in string.letters: u = u + x # u is just the letters from t v = "" for x in u: v = x + v # v is the reverse of u return u==v
2006/07 Computing Science 1P Lecture 18 - Simon Gay 7
Designing a GUI
Let's go for a layout along the following lines:
text entry area
label for result Check Quit
We'll work out the sizes by trial and error, but for more complexapplications it is normal to use a graphical layout tool to placethe desired widgets and automatically generate most of theGUI code.
2006/07 Computing Science 1P Lecture 18 - Simon Gay 8
Setting up the window and widgetsimport Tkinter
top = Tkinter.Tk()top.title("Palindrome Checker")top.geometry("300x50")
entry = Tkinter.Entry(top,width=48)entry.grid(row=0,column=0,columnspan=3)
resultLabel = Tkinter.Label(top,text="",width=34)resultLabel.grid(row=1,column=0)
checkButton = Tkinter.Button(top,text="Check")checkButton.grid(row=1,column=1)
quitButton = Tkinter.Button(top,text="Quit",command=top.destroy)quitButton.grid(row=1,column=2)
Tkinter.mainloop() gui1
2006/07 Computing Science 1P Lecture 18 - Simon Gay 9
A variable for the Entry widgettop = Tkinter.Tk()top.title("Palindrome Checker")top.geometry("300x50")
entryVar = Tkinter.StringVar("")
entry = Tkinter.Entry(top,width=48,textvariable=entryVar)entry.grid(row=0,column=0,columnspan=3)
resultLabel = Tkinter.Label(top,text="",width=34)resultLabel.grid(row=1,column=0)
checkButton = Tkinter.Button(top,text="Check")checkButton.grid(row=1,column=1)
quitButton = Tkinter.Button(top,text="Quit",command=top.destroy)quitButton.grid(row=1,column=2)
Tkinter.mainloop()
2006/07 Computing Science 1P Lecture 18 - Simon Gay 10
A callback for the Check buttontop = Tkinter.Tk()top.title("Palindrome Checker")top.geometry("300x50")
entryVar = Tkinter.StringVar("")
def check(): # definition of the function
entry = Tkinter.Entry(top,width=48,textvariable=entryVar)entry.grid(row=0,column=0,columnspan=3)
resultLabel = Tkinter.Label(top,text="",width=34)resultLabel.grid(row=1,column=0)
checkButton = Tkinter.Button(top,text="Check",command=check)checkButton.grid(row=1,column=1)
quitButton = Tkinter.Button(top,text="Quit",command=top.destroy)quitButton.grid(row=1,column=2)
2006/07 Computing Science 1P Lecture 18 - Simon Gay 11
Defining the check function
def check(): text = entryVar.get() result = palindrome(text) resultLabel.configure(text=result)
gui2
2006/07 Computing Science 1P Lecture 18 - Simon Gay 12
Improving the output
Instead of seeing 1 or 0, we would prefer an informativemessage.
def check(): text = entryVar.get() result = palindrome(text) if result: message = "It is a palindrome" else: message = "It is not a palindrome" resultLabel.configure(text=message)
gui3
2006/07 Computing Science 1P Lecture 18 - Simon Gay 13
Stylistic points
In general it is a good idea to define everything before it is used. It makes the program easier to read, and in any casemany programming languages insist on it.
Python is more flexible than most languages in this respect,but note that in GUI programs, the definition of a callbackfunction must appear before creating the widget which calls it.
def check(): # definition of the function
...
checkButton = Tkinter.Button(top,text="Check",command=check)checkButton.grid(row=1,column=1)
2006/07 Computing Science 1P Lecture 18 - Simon Gay 14
Stylistic points
It is also a good idea in general to put related parts of theprogram close to each other: for example, two functions whichdo related calculations, or one function which calls another.
In the case of GUI programs, does this mean:
1. Grouping all of the functions together, and all of the GUI partstogether; or
2. Grouping each widget with its callback function and anyrelated widgets?
2006/07 Computing Science 1P Lecture 18 - Simon Gay 15
import Tkinterfrom palindrome import *
top = Tkinter.Tk()top.title("Palindrome Checker")top.geometry("300x50")
entryVar = Tkinter.StringVar("")entry = Tkinter.Entry(top,width=48,textvariable=entryVar)entry.grid(row=0,column=0,columnspan=3)
resultLabel = Tkinter.Label(top,text="",width=34)resultLabel.grid(row=1,column=0)
def check(): text = entryVar.get() result = palindrome(text) if result: message = "It is a palindrome" else: message = "It is not a palindrome" resultLabel.configure(text=message)
checkButton = Tkinter.Button(top,text="Check",command=check)checkButton.grid(row=1,column=1)
quitButton = Tkinter.Button(top,text="Quit",command=top.destroy)quitButton.grid(row=1,column=2)
Tkinter.mainloop()
Is this the nicest layout? Not sure…
2006/07 Computing Science 1P Lecture 18 - Simon Gay 16
Pickling (book p81)
The module pickle provides functions for saving arbitrary data structures to a file, and loading them back in later.
Example
import pickle
# d is some data structure, e.g. a dictionary
f = open("store.pck","w")pickle.dump(d,f)f.close()
2006/07 Computing Science 1P Lecture 18 - Simon Gay 17
Picklingimport pickle
# d is some data structure, e.g. a dictionary
f = open("store.pck","w")pickle.dump(d,f)f.close()
import picklef1 = open("store.pck","r")d1 = pickle.load(f1)f1.close()
Later, maybe in another program:
2006/07 Computing Science 1P Lecture 18 - Simon Gay 18
More on function parameters
We are very familiar with the idea of defining a function withparameters:
def test(x,y,z):
and then calling the function with the correct number ofparameters in the correct order:
f(1,"hello",1.2)
So far, this is the norm in most programming languages.Python is unusually flexible in providing extra features.
2006/07 Computing Science 1P Lecture 18 - Simon Gay 19
Naming the parameters when calling a function
Optionally we can give the name of the parameter when wecall the function:
f(x=1,y="hello",z=1.2)
Why would we do this?
If the parameters have informative names, then the functioncall (as well as the function definition) becomes more readable:
def lookup(phonebook,name):
number = lookup(phonebook = myBook, name = "John")
2006/07 Computing Science 1P Lecture 18 - Simon Gay 20
More on naming parameters
If we name the parameters when calling a function, then wedon't have to put them in the correct order:
number = lookup(phonebook = myBook, name = "John")
number = lookup(name = "John", phonebook = myBook)
are both correct.
2006/07 Computing Science 1P Lecture 18 - Simon Gay 21
Default values of parameters
We can specify a default value for a parameter of a function.Giving a value to that parameter when calling the function thenbecomes optional.
def lookup(phonebook,name,errorvalue="")
Example:
then number = lookup(myBook, "John")
is equivalent to
number = lookup(myBook, "John", "")
2006/07 Computing Science 1P Lecture 18 - Simon Gay 22
Default values of parameters
We can specify a default value for a parameter of a function.Giving a value to that parameter when calling the function thenbecomes optional.
def lookup(phonebook,name,errorvalue="")
Example:
number = lookup(myBook, "John", "Error")
If we want to we can write