Guide to Programming with Python

Slides:



Advertisements
Similar presentations
Computer Science 112 Fundamentals of Programming II User Interfaces
Advertisements

Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
I210 review (for final exam) Fall 2011, IUB. What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
COMPSCI 101 Principles of Programming
Chapter 8: Writing Graphical User Interfaces
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Peter Andreae Python for Level 3 CS4HS see website: ecs.vuw.ac.nz/Main/PythonForSchools.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
The University of Texas – Pan American
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.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
PYTHON GUI PROGRAMMING
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)
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.
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1.
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.
JavaScript, Fourth Edition
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Tkinter Basics. Initial Stuff To get the package: from tkinter import * To make a window, give it a title and operate it: windowVar = Tk() windowVar.title(“your.
Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...
Creating visual interfaces in python
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
CS2021- Week 7 Tkinter Tour. Core tkinter concepts Widget Classes: Label Button Frame Toplevel, Tk Message, Entry, Checkbutton, Radiobutton Menubutton,
Computer Science 112 Fundamentals of Programming II Data Fields for I/O and Message Boxes for Error Recovery.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Topics Graphical User Interfaces Using the tkinter Module
PYGAME.
Graphical User Interfaces (GUIs)
Computer Science 111 Fundamentals of Programming I
Chapter Topics 15.1 Graphical User Interfaces
Chapter 8: Writing Graphical User Interfaces
Event loops 16-Jun-18.
Computer Science 112 Fundamentals of Programming II GUIs II:
Chap 7. Building Java Graphical User Interfaces
CISC101 Reminders Grading of Quiz 4 underway.
GUI Using Python.
Graphical User Interfaces -- Introduction
Fundamentals of Python: From First Programs Through Data Structures
Event Driven Programming
EE 422C Java FX.
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
Event loops.
I210 review.
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Fundamentals of Programming I Windows, Labels, and Command Buttons
Whatcha doin'? Aims: Begin to create GUI applications. Objectives:
Computer Science 111 Fundamentals of Programming I Text Areas
Computer Science 111 Fundamentals of Programming I User Interfaces
Topics Graphical User Interfaces Using the tkinter Module
Computer Science 111 Fundamentals of Programming I
Chapter 15: GUI Applications & Event-Driven Programming
Event loops 8-Apr-19.
The University of Texas – Pan American
Event loops.
Event loops 19-Aug-19.
Tkinter User Input Form
Presentation transcript:

Guide to Programming with Python Week13 Chapter Ten GUI Development: The Mad Lib Program

Objectives Events-driven programming! Work with a GUI toolkit Create and fill frames Create and use widgets Buttons text entries text boxes check buttons radio buttons Layout of the widget Create event handlers and bind (associate) events with event handlers

Understanding Event-Driven Programming Event-driven program: A program that responds to actions regardless of the order in which they occur (vs structured programming) Event: Something that happens involving a program's objects (mouse moves over; click of a button, etc) Event handler: Code that runs when a specific event occurs Bind: To associate an event with an event handler Event loop: A loop that checks for events and calls appropriate event handlers when they occur GUI programs traditionally event-driven: the users control the flow of the program

The Mad Lib Program Guide to Programming with Python

Buttons’ on Strike (I) (II) (III) (IV) from Tkinter import * root = Tk() #a root window, upon which other GUI elements can be added root.title("Simple GUI Demo") #add title root.geometry("400x200") #change the size of the window app = Frame(root) #create a frame, upon which other Widgets can be added app.grid() #need to invoke grid() method button1 = Button(app, text = "I am button #1") button1.grid() button2 = Button(app) button2.grid() button2.configure(text = "I am button #2”) button3 = Button(app, text = "I am button #3") button3.grid() button3['text'] = "I am button #3” root.mainloop() #must start up the window's event loop!!! (I) (II) (III) (IV)

(I) Creating a Root Window from Tkinter import * root = Tk() root.title("Simple GUI") root.geometry("200x100”) # takes a string To create a root window, instantiate object of the Tkinter class Tk Tkinter is a GUI module Imports all Tkinter into global scope Normally, avoid this kind of import * Some modules designed to be imported this way Saves typing and makes for cleaner code (no need to prefix the module name) Modify a window’s appearance by changing title and geometry

(II) Creating a Frame Master: A widget that contains other widgets app = Frame(root) app.grid() Master: A widget that contains other widgets Layout Manager: Controls arrangement of widgets Frame is widget that can hold other widgets When creating widget, must pass its master to constructor of new object Here, root is master that contains app grid() Method that all widgets have Associated with grid layout manager Guide to Programming with Python

(III) Using Widgets Widget: GUI elements (short for "window gadget") button1 = Button(app, text = "I am button #1") button1.grid() Widget: GUI elements (short for "window gadget") Button widget Is a button in GUI Can be activated by user to perform some action Button Class For a button widget Master is first argument passed to constructor text parameter for widget's text grid() method invoked ensures widget visible Guide to Programming with Python

(VI) Entering a Root Window’s Event Loop root.mainloop() Root window's event loop entered Window stays open, waiting to handle events Guide to Programming with Python

Widgets Widgets Description Example Label Uneditable text or icon Label(root, text=“I am a label”) Button A button that can be activated by the user to perform some action Button(root, text = “Go”) Text Allow user to input multiple lines Text(root, width=35,height=5,wrap=WORD) Entry Good for a line Entry(root) Check button Allow user to select choices from a group Checkbutton(root, text=“Comedy”) Radio button Radio buttons only allow one button in a group to be selected at once Radiobutton(root, text=“Comedy”)

Creating a GUI Using a Class Organizing code into classes can make programming easier Often beneficial to write larger GUI programs in OOP style Guide to Programming with Python

Defining the Application Class class Application(Frame): def __init__(self, master): # create a frame (need to get master 'root’) Frame.__init__(self, master) self.grid() self.CreateWidgets() def CreateWidgets(self): # create button #1 self.button1 = Button(self, text = "I am button #1”) self.button1.grid() … def main(): root = Tk() root.title("Simple GUI Demo") root.geometry("400x200”) app = Application(root) root.mainloop() main() # Application object is just specialized type of Frame object (derived from Frame)

Placing a Widget with the Grid Layout Manager Grid layout manager lets you place widgets at specific locations by treating frame as a grid of cells at row and column numbers

Widgets Have grid() Method def create_widgets(self): self.inst_lbl = Label(self, text = "Enter password for the secret of longevity") self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W) grid() method row takes integer, defines row object placed in master column takes integer, defines column object placed in master columnspan takes integer, defines width in columns sticky takes constants (including N, S, E, W), positions widget within cell Guide to Programming with Python

Binding Widgets and Event Handlers So far, GUI programs haven't had event handlers Widgets are like light fixtures without electrical wiring Write event handlers and bind them with events Guide to Programming with Python

Buttons That Do Something class Application(Frame): def __init__(self, master): ... self.bttn_clicks = 0 #initialize the button_clicks self.create_widgets() def create_widgets(self): self.button1 = Button(self, text = "Button #1 does nothing") self.button1.grid() self.button2 = Button(self) self.button2["text"] = "Button #2 counts: clicks=" + str(self.bttn_clicks) self.button2["command"] = self.update_count self.button2.grid() self.button3 = Button(self) self.button3.configure(text = "Button #3 resets") self.button3["command"] = self.clear_count self.button3.grid() def update_count(self): self.bttn_clicks += 1 def clear_count(self): self.bttn_clicks = 0

Creating the Event Handler def update_count(self): self.bttn_clicks += 1 self.button2["text"] = "Button #2 counts: clicks=" +\ str(self.bttn_clicks) update_count() increments total number of button clicks and changes text to reflect new total Guide to Programming with Python

Binding the Event Handler def create_widgets(self): self.button1 = Button(self, text = "Button #1 does nothing") self.button1.grid() self.button2 = Button(self) self.button2["text"] = "Button #2 counts: clicks=" +\ str(self.bttn_clicks) self.button2["command"] = self.update_count self.button2.grid() Set widget’s command option to bind activation of widget with event handler command option bound to update_count() method When button clicked, update_count() invoked Guide to Programming with Python

The Longevity Program Label Label Entry Button Text How the widgets are created? How the widgets are arranged? How the “Submit” button can do something?

Label Label Entry Button Text class Application(Frame): def __init__(self, master): … def create_widgets(self): self.inst_lbl = Label(self, text = "Enter password for the secret of longevity") self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W) self.pw_lbl = Label(self, text = "Password: ") self.pw_lbl.grid(row = 1, column = 0, sticky = W) self.pw_ent = Entry(self) self.pw_ent.grid(row = 1, column = 1, sticky = W) self.submit_bttn = Button(self, text = "Submit", command = self.reveal) self.submit_bttn.grid(row = 2, column = 0, sticky = W) self.secret_txt = Text(self, width = 35, height = 5, wrap = WORD) self.secret_txt.grid(row = 3, column = 0, columnspan = 2, sticky = W) def reveal(self): contents = self.pw_ent.get() if contents == "secret": #get the message here self.secret_txt.delete(0.0, END) self.secret_txt.insert(0.0, message) Label Label Entry Button Text

The Movie Chooser Program Label Checkbutton Text How to create the check buttons and associate them with the event?

class Application(Frame): def __init__(self, master): … def create_widgets(self): self.likes_comedy = BooleanVar() Checkbutton(self, text = "Comedy", variable = self.likes_comedy, command = self.update_text ).grid(row = 2, column = 0, sticky = W) self.results_txt = Text(self, width = 40, height = 5, wrap = WORD) self.results_txt.grid(row = 5, column = 0, columnspan = 3) def update_text(self): #get the message, likes if self.likes_comedy.get(): likes += "You like comedic movies.\n” …. self.results_txt.delete(0.0, END) self.results_txt.insert(0.0, likes)

Using Check Buttons self.likes_comedy = BooleanVar() Checkbutton(self, text = "Comedy", variable = self.likes_comedy, command = self.update_text ).grid(row = 2, column = 0, sticky = W) ... if self.likes_comedy.get(): likes += "You like comedic movies.\n” variable takes BooleanVar for status of check button BooleanVar Special class from Tkinter module Can reflect check button’s status (Can’t access the value directly; Must invoke object’s get() method command takes function or method to call when check button is checked or unchecked

Using Radio Buttons Radio buttons allow user to select one from a group of choices Radio buttons (instead of check buttons) The Movie Chooser 2 Program Guide to Programming with Python

Using Radio Buttons self.favorite = StringVar() StringVar Radiobutton(self, text = "Comedy", variable = self.favorite, value = "comedy.", command = self.update_text ).grid(row = 2, column = 0, sticky = W) ... message += self.favorite.get() StringVar Special class from Tkinter module; required by Radiobutton objects Can reflect status of a group of radio buttons variable parameter gets StringVar self.favorite The same StringVar object passed to all the radio buttons of the same group When radio button is selected, StringVar assigned string referenced by object’s value option When Comedy radio button selected, self.favorite gets "comedy."

Summary A GUI is a graphical user interface A widget, short for window gadget, is a GUI element A master widget (frame) contains other widgets A layout manager controls the arrangement of widgets (grid()) An event-driven program responds to actions regardless of the order in which they occur An event is something that happens involving a program’s objects, and needs to be associated with event handlers Guide to Programming with Python