Download presentation
Presentation is loading. Please wait.
Published byLenard Moody Modified over 9 years ago
1
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07
2
Computing Science 1P Lecture 18 - Simon Gay2 Announcement On Wednesday next week three non-standard things will happen. The first is that the class will be taken by Peter Saffrey, as I have to be away. Peter is one of our most experienced tutors, and some of you know him from the accelerator course. He will cover some useful examples, and you should also find it helpful to hear from someone different for a change.
3
2006/07Computing Science 1P Lecture 18 - Simon Gay3 Announcement On Wednesday next week three non-standard things will happen. The second is that Steve Draper from the Psychology department will be running an evaluation of CS1P, after the lecture. This is to help us with revisions to the new version of the module. If you volunteer to help with the evaluation, it will be very helpful to us (and you will get a free lunch). Steve will arrive at the end of this lecture to say more about it.
4
2006/07Computing Science 1P Lecture 18 - Simon Gay4 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.
5
2006/07Computing Science 1P Lecture 18 - Simon Gay5 Another GUI example Recall one of the first semester's exercises: working out whether or not a given text is a palindrome (ignoring spaces and punctuation). Let's put a graphical user interface on it. First we'll reconstruct the original solution.
6
2006/07Computing Science 1P Lecture 18 - Simon Gay6 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
7
2006/07Computing Science 1P Lecture 18 - Simon Gay7 Designing a GUI Let's go for a layout along the following lines: text entry area label for resultCheck Quit We'll work out the sizes by trial and error, but for more complex applications it is normal to use a graphical layout tool to place the desired widgets and automatically generate most of the GUI code.
8
2006/07Computing Science 1P Lecture 18 - Simon Gay8 Setting up the window and widgets import 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
9
2006/07Computing Science 1P Lecture 18 - Simon Gay9 A variable for the Entry widget 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) 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()
10
2006/07Computing Science 1P Lecture 18 - Simon Gay10 A callback for the Check button top = 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)
11
2006/07Computing Science 1P Lecture 18 - Simon Gay11 Defining the check function def check(): text = entryVar.get() result = palindrome(text) resultLabel.configure(text=result) gui2
12
2006/07Computing Science 1P Lecture 18 - Simon Gay12 Improving the output Instead of seeing 1 or 0, we would prefer an informative message. 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
13
2006/07Computing Science 1P Lecture 18 - Simon Gay13 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 case many 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 callback function 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)
14
2006/07Computing Science 1P Lecture 18 - Simon Gay14 Stylistic points It is also a good idea in general to put related parts of the program close to each other: for example, two functions which do 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 parts together; or 2.Grouping each widget with its callback function and any related widgets?
15
2006/07Computing Science 1P Lecture 18 - Simon Gay15 import Tkinter from 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…
16
2006/07Computing Science 1P Lecture 18 - Simon Gay16 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()
17
2006/07Computing Science 1P Lecture 18 - Simon Gay17 Pickling import pickle # d is some data structure, e.g. a dictionary f = open("store.pck","w") pickle.dump(d,f) f.close() import pickle f1 = open("store.pck","r") d1 = pickle.load(f1) f1.close() Later, maybe in another program:
18
2006/07Computing Science 1P Lecture 18 - Simon Gay18 More on function parameters We are very familiar with the idea of defining a function with parameters: def test(x,y,z): and then calling the function with the correct number of parameters 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.
19
2006/07Computing Science 1P Lecture 18 - Simon Gay19 Naming the parameters when calling a function Optionally we can give the name of the parameter when we call the function: f(x=1,y="hello",z=1.2) Why would we do this? If the parameters have informative names, then the function call (as well as the function definition) becomes more readable: def lookup(phonebook,name): number = lookup(phonebook = myBook, name = "John")
20
2006/07Computing Science 1P Lecture 18 - Simon Gay20 More on naming parameters If we name the parameters when calling a function, then we don't have to put them in the correct order: number = lookup(phonebook = myBook, name = "John") number = lookup(name = "John", phonebook = myBook) are both correct.
21
2006/07Computing Science 1P Lecture 18 - Simon Gay21 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 then becomes optional. def lookup(phonebook,name,errorvalue="") Example: then number = lookup(myBook, "John") is equivalent to number = lookup(myBook, "John", "")
22
2006/07Computing Science 1P Lecture 18 - Simon Gay22 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 then becomes optional. def lookup(phonebook,name,errorvalue="") Example: number = lookup(myBook, "John", "Error") If we want to we can write
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.