Chapter 8 Flashcards
Graphical User Interfaces (GUI)
Displays text, small images (icons), that represent objects such as directories, files of diff types, command buttons and drop-down menus
Terminal-Based version
- user is constrained to reply to a definite sequence of prompts for inputs - once input entered, no way to change it
- To obtain results for a different set of input data, user must wait for command menu
to be displayed again - At that point, the same command and all of the other inputs must be re‐entered
GUI- Based version
- displays window that contains various components - window objects or widgets
- effects on users
a. user not constrained to enter inputs in particular order - Before pressing the Compute button, user can edit any of the data
b. Running different data sets does not require re‐entering all of the data
Event-Driven Programming
User- generated events (e.g., mouse clicks) trigger ops in program to respond by pulling in inputs, processing them, and displaying results
- Event-driven software
- Event-driven programming
Event-Driven Programming Coding phase
- Define a new class to represent the main window
- Instantiate the classes of window objects needed for this application (e.g., labels,
command buttons) - Position these components in the window
- Instantiate the data model and provide any default data in the window objects
- Register controller methods with each window object in which a relevant event might
occur - Define these controller methods
- Define a main that launches the GUI
tkinter
module that includes classes for windows and numerous types of window objects
breezypythongui
custom, open-source module
EasyFrame
- class that provides the basic functionality for any window, such as the command buttons in the title bar
- a new window class extends it (meaning provides additional functionality
- subclass of tkinter’s Frame class
Template of GUI program
from breezypythongui import EasyFrame
Other imports
class ApplicationName(EasyFrame):
The __init__ method definition
Definitions of event handling methods
def main():
ApplicationName().mainloop()
if __name__ == “__main__”:
main()
Syntax of Class and Method Definitions
- Each def has a 1 line header beginning with keyword (class/def) - followed by body of code indented 1 level in the text
- class header: name of class folowed by parenthesized list of one or more parent classes
- body: nested 1 tab under header, 1 or more method defs
- method header: looks like function header but always has at least 1 parameter named self
subclass
- class that inherits attributes & behaviors from another class
ie.
EasyFrame is a subclass of Frame, and classes LabelDemo or whatever new class is created is a subclass of EasyFrame
Window attributes
- Title (an empty string by default)
- Width & height in pixels
- resizability (true by default)
- background color (white by default)
Window layout
- window components laid out in 2 dimensional grid - rows/columns numbered from position (0,0) in upper left corner of window
- each type of window component has default alignment
- default alignment can be overrode with “sticky”
- an aspect of window layout involves spanning of a window component across several grid positions
- horizontal/vert spanning of grid can be forced by supplying rowspan and columnspan
Methods in breezypythongui
- methods for adding each type of window component to a window
- each method uses this form:
self.addComponentType(<arguments>)</arguments>
What happens when a method in breezypythongui is called?
- breezypythongui creates an instance of the requested type of window component
- initializes component’s attributes with default values or any values provided by programmer
- places the component in its grid position (row & column are required arguments)
- returns a reference to the component