Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.

Slides:



Advertisements
Similar presentations
CUSTOMIZING VQC Apr-17 N10 (CATEGORY) (NAME SLOTS) N11 (TEMPLATE)
Advertisements

Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Recitation 1 Programming for Engineers in Python.
Group practice in problem design and problem solving
Fundamentals of Python: From First Programs Through Data Structures
Graphical User Interfaces in Haskell Koen Lindström Claessen.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Fundamentals of Python: First Programs
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
General Programming Introduction to Computing Science and Programming I.
Instructor: Tina Tian. About me Office: RLC 203A Office Hours: Wednesday 1:30 - 4:30 PM or .
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
PC204 Lecture 9 Conrad Huang Genentech Hall, N453A x
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Computer Science 112 Fundamentals of Programming II Command Buttons and Responding to Events.
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Streams Using Python ByTim Walsh. What is a stream? Called a “generator” in Python Similar to a list First made available in Python 2.2, all newer versions.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 19: Friday 9 th March Simon Gay Department of Computing Science University of Glasgow 2006/07.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Creating visual interfaces in python
GUIs Basic Concepts. GUI GUI : Graphical User Interface Window/Frame : a window on the screen Controls/Widgets : GUI components.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Guide to Programming with Python
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Introduction to Programming Python Lab 1: My First Program 8 January PythonLab1 lecture slides.ppt Ping Brennan
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
RAPID: THE RAPID ADA PORTABLE INTERFACE DESIGNER Martin Carlisle Department of Computer Science US Air Force Academy.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Development Environment
Introduction to Computing Science and Programming I
Introduction to Programming
Topics Graphical User Interfaces Using the tkinter Module
Graphical User Interfaces (GUIs)
Functions CIS 40 – Introduction to Programming in Python
Graphical User Interfaces (GUIs)
CISC101 Reminders Grading of Quiz 4 underway.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
I210 review.
Introduction to Programming
Whatcha doin'? Aims: Begin to create GUI applications. Objectives:
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Topics Graphical User Interfaces Using the tkinter Module
A Level Computer Science Topic 6: Introducing OOP
Introduction to Programming
CISC101 Reminders Assignment 3 due today.
Lab 8: GUI testing Software Testing LTAT
Presentation transcript:

Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07

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.

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.

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.

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.

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

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.

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

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()

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)

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

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

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)

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?

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…

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()

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:

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.

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")

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.

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", "")

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