Computer Science 112 Fundamentals of Programming II Command Buttons and Responding to Events.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Computer Science 112 Fundamentals of Programming II User Interfaces
Chapter 8 Improving the User Interface
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
Excel and VBA Creating an Excel Application
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.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
The Bean Counter: A JavaScript Program
Welcome to CIS 083 ! Events CIS 068.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Chapter 8: Writing Graphical User Interfaces
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 112 Fundamentals of Programming II Graphics Programming.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
IOS with Swift Hello world app.
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.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
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.
Illustration of a Visual Basic Program Running an Ada Program 1 by Richard Conn 11 September 1999.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Topics Introduction Scene Graphs
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.
VAT Calculator program Controls Properties Code Results.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
Guide to Programming with Python
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
Customizing Menus and Toolbars CHAPTER 12 Customizing Menus and Toolbars.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
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.
Computer Science 112 Fundamentals of Programming II Data Fields for I/O and Message Boxes for Error Recovery.
Topics Graphical User Interfaces Using the tkinter Module
PYGAME.
Graphical User Interfaces (GUIs)
Computer Science 111 Fundamentals of Programming I
Introduction to Event-Driven Programming
Chapter Topics 15.1 Graphical User Interfaces
Chapter 8: Writing Graphical User Interfaces
Event loops 16-Jun-18.
CHAPTER FIVE Decision Structures.
Fundamentals of Programming I The Model/View/Controller Design Pattern
Computer Science 112 Fundamentals of Programming II GUIs II:
Fundamentals of Python: From First Programs Through Data Structures
Event Driven Programming
CHAPTER FIVE Decision Structures.
Tkinter GUIs Computer Science and Software Engineering
Variables and Arithmetic Operations
Fundamentals of Python: First Programs Second Edition
Event loops.
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
Fundamentals of Programming I The Model/View/Controller Design Pattern
Computer Science 111 Fundamentals of Programming I
Event loops 8-Apr-19.
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

Computer Science 112 Fundamentals of Programming II Command Buttons and Responding to Events

Event-Driven Programming 1.Layout and pop up a window 2.Wait for events 3.When an event occurs, respond 4.Goto step 2

Types of Events Button click Menu item selection Check box selection List box selection Keyboard entry Mouse move Mouse press Mouse release Mouse drag

Hello world! Revisited

Command Buttons The method addButton adds a widget of type tkinter.Button to the window and returns it Text, row, and column are required args Button attributes –text (the button’s label) –image (for “hot spots”) –state (“normal” or “disabled”) –command (a method to run when the button is clicked)

Methods as Attributes? Python methods and functions are first-class data objects They can be passed as arguments to other methods and functions, and returned as the values of other methods and functions The default command method for a button is a method of no arguments that does nothing

class HelloWorld(EasyFrame): """Illustrates buttons and user events.""" def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self) self.label = self.addLabel(text = "Hello world!", row = 0, column = 0, columnspan = 2, sticky = "NSEW")

class HelloWorld(EasyFrame): """Illustrates buttons and user events.""" def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self) self.label = self.addLabel(text = "Hello world!", row = 0, column = 0, columnspan = 2, sticky = "NSEW") # Add command buttons self.clearBtn = self.addButton(text = "Clear", row = 1, column = 0) self.restoreBtn = self.addButton(text = "Restore", row = 1, column = 1, state = "disabled") We can now view the layout, but the buttons can’t respond

class HelloWorld(EasyFrame): """Illustrates buttons and user events.""" def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self) self.label = self.addLabel(text = "Hello world!", row = 0, column = 0, columnspan = 2, sticky = "NSEW") # Add command buttons self.clearBtn = self.addButton(text = "Clear", row = 1, column = 0, command = self.clear) self.restoreBtn = self.addButton(text = "Restore", row = 1, column = 1, command = self.restore, state = "disabled") The method’s names are here used just like variable references

class HelloWorld(EasyFrame): """Illustrates buttons and user events.""" def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self) self.label = self.addLabel(text = "Hello world!", row = 0, column = 0, columnspan = 2, sticky = "NSEW") # Add command buttons self.clearBtn = self.addButton(text = "Clear", row = 1, column = 0, command = self.clear) self.restoreBtn = self.addButton(text = "Restore", row = 1, column = 1, command = self.restore, state = "disabled") # Methods to handle user events def clear(self): """Resets the label to the empty string and inverts the button states.""" self.label["text"] = "" self.clearBtn["state"] = "disabled" self.restoreBtn["state"] = "normal"

def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self) self.label = self.addLabel(text = "Hello world!", row = 0, column = 0, columnspan = 2, sticky = "NSEW") # Add command buttons self.clearBtn = self.addButton(text = "Clear", row = 1, column = 0, command = self.clear) self.restoreBtn = self.addButton(text = "Restore", row = 1, column = 1, command = self.restore, state = "disabled") # Methods to handle user events def clear(self): """Resets the label to the empty string and inverts the button states.""" self.label["text"] = "" self.clearBtn["state"] = "disabled" self.restoreBtn["state"] = "normal" def restore(self): """Resets the label to 'Hello world!' and inverts the state of the buttons.""" self.label["text"] = "Hello world!" self.clearBtn["state"] = "normal" self.restoreBtn["state"] = "disabled"

Command Buttons and Event Handling Usually, each button has its own dedicated event handling method Kind of like an automatic if statement The GUI also automatically runs an event-driven loop, behind the scenes

A Calculator Prototype Can enter a number, but no other functions yet

class CalculatorDemo(EasyFrame): """Illustrates command buttons and user events.""" def __init__(self): """Sets up the window, label, and buttons.""" EasyFrame.__init__(self, "Calculator") self.digits = self.addLabel("0", row = 0, column = 0, columnspan = 3, sticky = "NSEW") digit = 9 for row in range(1, 4): for column in range(0, 3): button = self.addButton(str(digit), row, column) digit -= 1 # Instantiates and pops up the window. if __name__ == "__main__": CalculatorDemo().mainloop() Always lay out and pop up the window to refine the look of the UI, before you write the event handlers

Many Event Handlers, or One? Usually, each button has its own dedicated event handling method But, in this case, the method just adds the button’s text to the label’s text If we can find a way to access that text, we can use one event handling method for all the buttons

Solution Define a method that expects the button’s text as an argument Run that method when the button’s command attribute is set The method defines a nested function of no arguments, which adds the button’s text to the label’s text The method returns the function, which becomes the event handler for that button

def __init__(self): """Sets up the window, label, and buttons.""" EasyFrame.__init__(self, "Calculator") self.digits = self.addLabel("0", row = 0, column = 0, columnspan = 3, sticky = "NSEW") digit = 9 for row in range(1, 4): for column in range(0, 3): button = self.addButton(str(digit), row, column) button["command"] = self.makeCommand(str(digit)) digit -= 1 self.makeCommand is a method that defines and returns a function of no arguments

def __init__(self): """Sets up the window, label, and buttons.""" EasyFrame.__init__(self, "Calculator") self.digits = self.addLabel("0", row = 0, column = 0, columnspan = 3, sticky = "NSEW") digit = 9 for row in range(1, 4): for column in range(0, 3): button = self.addButton(str(digit), row, column) button["command"] = self.makeCommand(str(digit)) digit -= 1 # Event handling method builder for digit buttons def makeCommand(self, buttonText): """Define and return the event handler for a button.""" def addDigit(): if self.digits["text"] == "0": self.digits["text"] = "" self.digits["text"] += buttonText return addDigit The function addDigit has access to the button’s text

For Friday Input and output with data fields Responding to error conditions