Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.

Slides:



Advertisements
Similar presentations
Chapter 8 Improving the User Interface
Advertisements

Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Chapter 14: Event-Driven Programming with Graphical User Interfaces
Chapter 7 Improving the User Interface
MCT260-Operating Systems I Operating Systems I Interfaces to Operating Systems.
Fundamentals of Python: From First Programs Through Data Structures
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
Visual Basic Chapter 1 Mr. Wangler.
Fundamentals of Python: First Programs
Chapter 1 P. 1 Writing Windows applications with Visual Basic Figure 1.1 The first program works as follows: (These operations can be performed in any.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
PC204 Lecture 9 Conrad Huang Genentech Hall, N453A x
PYTHON GUI PROGRAMMING
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
University of Sunderland CIF 102/FIF102 Fundamentals of DatabasesUnit 15 Programming in Microsoft Access using VBA Using VBA to add functionality.
Computer Science 112 Fundamentals of Programming II Command Buttons and Responding to Events.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 20 Graphical User Interface (GUI)
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
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.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
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.
Creating visual interfaces in python
GUIs Basic Concepts. GUI GUI : Graphical User Interface Window/Frame : a window on the screen Controls/Widgets : GUI components.
Graphical User Interface Saint-Petersburg IT College Saint-Petersburg 2014.
Guide to Programming with Python
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
COMPSA Exam Prep Session by Paul Allison On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Programming Logic and Design Seventh Edition Chapter 12 Event-Driven GUI Programming, Multithreading, and Animation.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Topics Graphical User Interfaces Using the tkinter Module
PYGAME.
Graphical User Interfaces (GUIs)
Chapter Topics 15.1 Graphical User Interfaces
Event loops 16-Jun-18.
Fundamentals of Python: From First Programs Through Data Structures
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
Event loops.
GRAPHICAL USER INTERFACE
I210 review.
Introduction to Programming
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Fundamentals of Programming I Windows, Labels, and Command Buttons
Computer Science 111 Fundamentals of Programming I User Interfaces
Topics Graphical User Interfaces Using the tkinter Module
Event loops 8-Apr-19.
Event loops.
Introduction to Programming
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012

Python Programming (GUI) - Saad Bani Mohammad2 Graphical User Interfaces The key ideas of graphical user interfaces (on-screen windows, icons, menus, buttons etc, and a pointing device (mouse)) were developed at Xerox PARC during the late 1970s. These ideas were adopted by Apple, first in the Lisa (1983) and then in the popular Macintosh (1984). In 1985, Microsoft introduced Windows, first as an application and later as an operating system. Python makes it very easy to implement simple GUIs, so we will look at the main points.

Python Programming (GUI) - Saad Bani Mohammad3 The main ideas Some of the programs we have written have almost no user interface, or at least take no input from the user. Most of the programs we have written are just a sequence of statements, carrying out the computation. The structure of the program is a loop, prompting the user for a command and then calling a function in order to carry it out. GUI programs have a similar structure, except that the main loop is provided by a module, and the functions doing the work are called in response to mouse clicks on buttons, etc.

Python Programming (GUI) - Saad Bani Mohammad4 Where to find more information We will be using the Tkinter module for GUI programming. It is not covered in the course textbook. It will be in the exam. Many other Python books cover GUI programming with Tkinter, although they tend to make the simple examples more complicated than they need to be. A useful reference can be found at although again, its examples are over-complicated. Other links:

Python Programming (GUI) - Saad Bani Mohammad5 The simplest GUI program in Python # Use the Tkinter module import Tkinter # Create the top-level (or root) window top = Tkinter.Tk() # Create a button... quitButton = Tkinter.Button(top,text="Quit", command=top.destroy) #... and display it in the window quitButton.grid() # Start the main loop: responds to the mouse etc Tkinter.mainloop() example1

Python Programming (GUI) - Saad Bani Mohammad6 Line by line top = Tkinter.Tk() This must be present in order to create a window. You always need at least one window to put buttons etc. in.

Python Programming (GUI) - Saad Bani Mohammad7 Line by line quitButton = Tkinter.Button(top,text="Quit", command=top.destroy) this must be present, to associate the button with the root window optional specifies a callback to be used when the button is pressed

Python Programming (GUI) - Saad Bani Mohammad8 Line by line quitButton.grid() This uses the layout manager called grid to place the button in the root window. Without this line, the button will not be displayed.

Python Programming (GUI) - Saad Bani Mohammad9 Line by line Tkinter.mainloop() This starts the main loop, which tracks the mouse and works out when and where it has been pressed. Clicking the mouse on the Quit button causes the callback to be called: i.e. the method top.destroy is called, which terminates the root window. In some books you will see top.mainloop() instead; it doesn't matter.

Python Programming (GUI) - Saad Bani Mohammad10 Event-driven programming GUI applications use a style of programming called event-driven. Events are mouse movements, mouse clicks, key presses, and many higher-level events constructed from these. (For example, clicking the mouse while the pointer is over a button generates a button press event). Some events are handled completely within the main loop provided by Tkinter. (For example, mouse movements are used to update the position of the pointer on the screen; clicking the minimize button of the window has the usual effect.)

Python Programming (GUI) - Saad Bani Mohammad11 Event-driven programming Other events, usually higher-level events such as button presses, menu selections, typing in a text field, are handled in a way that involves the user's code. This is controlled by defining callbacks. Example: if we have a button, the event we are interested in is pressing it. When the button is created, the command parameter is used to specify which function to call when the button is pressed. quitButton = Tkinter.Button(top,text="Quit", command=top.destroy)

Python Programming (GUI) - Saad Bani Mohammad12 Extending the example First let's add something to enable us to display a message to the user. Tkinter provides Label for this purpose. import Tkinter top = Tkinter.Tk() messageLabel = Tkinter.Label(top,text="Hello World!") messageLabel.grid() quitButton = Tkinter.Button(top,text="Quit", command=top.destroy) quitButton.grid() Tkinter.mainloop() example2

Python Programming (GUI) - Saad Bani Mohammad13 Extending the example Instead of displaying the message immediately, let's add another button with a callback which will display the message. import Tkinter def display(): messageLabel.configure(text="Hello World!") top = Tkinter.Tk() messageLabel = Tkinter.Label(top,text="") messageLabel.grid() showButton = Tkinter.Button(top,text="Show",command=display) showButton.grid() quitButton = Tkinter.Button(top,text="Quit",command=top.destroy) quitButton.grid() Tkinter.mainloop() example3

Python Programming (GUI) - Saad Bani Mohammad14 Points to note import Tkinter def display(): messageLabel.configure(text="Hello World!") top = Tkinter.Tk() messageLabel = Tkinter.Label(top,text="") messageLabel.grid() showButton = Tkinter.Button(top,text="Show",command=display) showButton.grid() quitButton = Tkinter.Button(top,text="Quit",command=top.destroy) quitButton.grid() Tkinter.mainloop() no brackets definition before use

Python Programming (GUI) - Saad Bani Mohammad15 Terminology The generic term for a GUI element (button, menu, label, …) is a widget.

Python Programming (GUI) - Saad Bani Mohammad16 Changing the layout We can use optional arguments with the grid method to control how widgets are placed. import Tkinter def display(): messageLabel.configure(text="Hello World!") top = Tkinter.Tk() messageLabel = Tkinter.Label(top,text="",width=12) messageLabel.grid(row=0,column=0) showButton = Tkinter.Button(top,text="Show",command=display) showButton.grid(row=0,column=1) quitButton = Tkinter.Button(top,text="Quit",command=top.destroy) quitButton.grid(row=0,column=2) Tkinter.mainloop() example4

Python Programming (GUI) - Saad Bani Mohammad17 Getting input from the user import Tkinter def display(): name = textVar.get() messageLabel.configure(text="Hello "+name) top = Tkinter.Tk() textVar = Tkinter.StringVar("") textEntry = Tkinter.Entry(top,textvariable=textVar,width=12) textEntry.grid(row=0,column=0) messageLabel = Tkinter.Label(top,text="",width=12) messageLabel.grid(row=1,column=0) showButton = Tkinter.Button(top,text="Show",command=display) showButton.grid(row=1,column=1) quitButton = Tkinter.Button(top,text="Quit",command=top.destroy) quitButton.grid(row=1,column=2) Tkinter.mainloop() example5

Python Programming (GUI) - Saad Bani Mohammad18 Important idea The Entry widget allows the user to enter text, but it does not have storage for the text built in. We have to create a Tkinter.StringVar object and give it to the Entry object. We can then use the get method of the StringVar to obtain the text. This style of programming is also needed with several other Tkinter widgets. It must be a StringVar, not an ordinary string variable.

Python Programming (GUI) - Saad Bani Mohammad19 Another example: Radiobutton def display(): name = textVar.get() ch = choice.get() if ch == 1: message = "Hello "+name elif ch == 2: message = "Goodbye "+name else: message = "" messageLabel.configure(text=message) top = Tkinter.Tk() textVar = Tkinter.StringVar("") textEntry = Tkinter.Entry(top,textvariable=textVar,width=12) textEntry.grid(row=0,column=0) messageLabel = Tkinter.Label(top,text="",width=12) messageLabel.grid(row=1,column=0) choice = Tkinter.IntVar(0) helloButton = Tkinter.Radiobutton(top,text="Hello", variable=choice,value=1,command=display) helloButton.grid(row=1,column=1) goodbyeButton = Tkinter.Radiobutton(top,text="Goodbye", variable=choice,value=2,command=display) goodbyeButton.grid(row=1,column=2) quitButton = Tkinter.Button(top,text="Quit",command=top.destroy) quitButton.grid(row=1,column=3) Tkinter.mainloop() example6

Python Programming (GUI) - Saad Bani Mohammad20 Another example: Radiobutton def display(): name = textVar.get() ch = choice.get() if ch == 1: message = "Hello "+name elif ch == 2: message = "Goodbye "+name else: message = "" messageLabel.configure(text=message) top = Tkinter.Tk() textVar = Tkinter.StringVar("") textEntry = Tkinter.Entry(top,textvariable=textVar,width=12) textEntry.grid(row=0,column=0) messageLabel = Tkinter.Label(top,text="",width=12) messageLabel.grid(row=1,column=0) choice = Tkinter.IntVar(0) helloButton = Tkinter.Radiobutton(top,text="Hello", variable=choice,value=1,command=display) helloButton.grid(row=1,column=1) goodbyeButton = Tkinter.Radiobutton(top,text="Goodbye", variable=choice,value=2,command=display) goodbyeButton.grid(row=1,column=2) quitButton = Tkinter.Button(top,text="Quit",command=top.destroy) quitButton.grid(row=1,column=3) Tkinter.mainloop()

Python Programming (GUI) - Saad Bani Mohammad21 Another example: Radiobutton def display(): name = textVar.get() ch = choice.get() if ch == 1: message = "Hello "+name elif ch == 2: message = "Goodbye "+name else: message = "" messageLabel.configure(text=message) top = Tkinter.Tk() textVar = Tkinter.StringVar("") textEntry = Tkinter.Entry(top,textvariable=textVar,width=12) textEntry.grid(row=0,column=0) messageLabel = Tkinter.Label(top,text="",width=12) messageLabel.grid(row=1,column=0) choice = Tkinter.IntVar(0) helloButton = Tkinter.Radiobutton(top,text="Hello", variable=choice,value=1,command=display) helloButton.grid(row=1,column=1) goodbyeButton = Tkinter.Radiobutton(top,text="Goodbye", variable=choice,value=2,command=display) goodbyeButton.grid(row=1,column=2) quitButton = Tkinter.Button(top,text="Quit",command=top.destroy) quitButton.grid(row=1,column=3) Tkinter.mainloop()

22 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. Python Programming (GUI) - Saad Bani Mohammad

23 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 Python Programming (GUI) - Saad Bani Mohammad

24 Designing a GUI Let's go for a layout along the following lines: text entry area label for resultCheck Quit Python Programming (GUI) - Saad Bani Mohammad

25 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) # columnspan specifies how many cell columns of the table this cell should span. 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 Python Programming (GUI) - Saad Bani Mohammad

26 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() Python Programming (GUI) - Saad Bani Mohammad

27 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) Python Programming (GUI) - Saad Bani Mohammad

28 Defining the check function def check(): text = entryVar.get() result = palindrome(text) resultLabel.configure(text=result) gui2 Python Programming (GUI) - Saad Bani Mohammad

29 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 Python Programming (GUI) - Saad Bani Mohammad

30 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) Python Programming (GUI) - Saad Bani Mohammad

31 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? Python Programming (GUI) - Saad Bani Mohammad

32 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… Python Programming (GUI) - Saad Bani Mohammad