Fundamentals of Python: From First Programs Through Data Structures

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Computer Science 112 Fundamentals of Programming II User Interfaces
Chapter 8 Improving the User Interface
Chapter 7 Improving the User Interface
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
Visual Basic 2005 CHAPTER 2 Program and Graphical User Interface Design.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual 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)
Chapter 2 – Introduction to the Visual Studio .NET IDE
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1.
COMPUTER APPLICATIONS COURSE LEARN HOW TO USE COMPUTERS.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Microsoft Visual Basic 2010 CHAPTER TWO Program and Graphical User Interface Design.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to the Visual Studio.NET IDE Outline 2.1Introduction 2.2Visual Studio.NET Integrated.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
 You won’t write a single line of program code.  Instead, you’ll use visual programming techniques.  Visual Studio processes your actions (such as mouse.
DB Implementation: MS Access Forms. MS Access Forms: Purpose Data entry, editing, & viewing data in Tables Forms are user-friendlier to end-users than.
Dive Into® Visual Basic 2010 Express
Excel Tutorial 8 Developing an Excel Application
Java FX: Scene Builder.
Topics Graphical User Interfaces Using the tkinter Module
Event Loops and GUI Intro2CS – weeks
Chapter 1: An Introduction to Visual Basic 2015
A First Look at GUI Applications
Computer Science 111 Fundamentals of Programming I
Chapter 8: Writing Graphical User Interfaces
Building a User Interface with Forms
Event loops 16-Jun-18.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Program and Graphical User Interface Design
Fundamentals of Programming I The Model/View/Controller Design Pattern
Microsoft Word 2003 Illustrated Complete
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
DB Implementation: MS Access Forms
GUI Using Python.
Graphical User Interfaces -- Introduction
Program and Graphical User Interface Design
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
Variables and Arithmetic Operations
Fundamentals of Python: First Programs Second Edition
Chapter 2 – Introduction to the Visual Studio .NET IDE
Event loops.
Tutorial 6 Creating Dynamic Pages
DREAMWEAVER MX 2004 Chapter 3 Working with Tables
GRAPHICAL USER INTERFACE
DB Implementation: MS Access Forms
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 User Interfaces
Fundamentals of Programming I The Model/View/Controller Design Pattern
Topics Graphical User Interfaces Using the tkinter Module
Event loops 8-Apr-19.
Exploring Microsoft® Office 2016 Series Editor Mary Anne Poatsy
Key Applications Module Lesson 14 — Working with Tables
Event loops.
Event loops 19-Aug-19.
Chapter 4 Enhancing the Graphical User Interface
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Fundamentals of Python: From First Programs Through Data Structures Chapter 9 Graphical User Interfaces

Objectives After completing this chapter, you will be able to: Structure a GUI-based program using the model/view/controller pattern Instantiate and lay out different types of window objects, such as labels, entry fields, and command buttons, in a window’s frame Define methods that handle events associated with window objects Organize sets of window objects in nested frames Fundamentals of Python: From First Programs Through Data Structures

Introduction Most modern computer software employs a graphical user interface or GUI A GUI displays text as well as small images (called icons) that represent objects such as directories, files of different types, command buttons, and drop-down menus In addition to entering text at keyboard, the user of a GUI can select an icon with pointing device, such as mouse, and move that icon around on the display Fundamentals of Python: From First Programs Through Data Structures

The Behavior of Terminal-Based Programs and GUI-Based Programs Two different versions of the bouncy program from a user’s point of view: Terminal-based user interface Graphical user interface Both programs perform exactly the same function However, their behavior, or look and feel, from a user’s perspective are quite different Fundamentals of Python: From First Programs Through Data Structures

The Terminal-Based Version Fundamentals of Python: From First Programs Through Data Structures

The Terminal-Based Version (continued) Problems: User is constrained to reply to a definite sequence of prompts for inputs Once an input is entered, there is 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 User can enter an unrecognized command Fundamentals of Python: From First Programs Through Data Structures

The GUI-Based Version Uses a window that contains various components Called window objects or widgets Solves problems of terminal-based version Title bar A command button A label An entry field Can be dragged to resize window Fundamentals of Python: From First Programs Through Data Structures

Event-Driven Programming User-generated events (e.g., mouse clicks) trigger operations in program to respond by pulling in inputs, processing them, and displaying results Event-driven software Event-driven programming Fundamentals of Python: From First Programs Through Data Structures

Event-Driven Programming (continued) 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 for the display of 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 Fundamentals of Python: From First Programs Through Data Structures

Coding Simple GUI-Based Programs There are many libraries and toolkits of GUI components available to the Python programmer For example, Tkinter and tkMessageBox Standard Python modules Tkinter includes classes for windows and numerous types of window objects tkMessageBox includes functions for several standard pop-up dialog boxes Fundamentals of Python: From First Programs Through Data Structures

Windows and Labels A grid layout allows programmer to place components in the cells of an invisible grid Parent component for Label object Fundamentals of Python: From First Programs Through Data Structures

Windows and Labels (continued) The GUI is launched in the main method Instantiates LabelDemo and calls mainloop mainloop method pops up window and waits for user events At this point, the main method quits (GUI is running a hidden, event-driven loop in a separate process) Fundamentals of Python: From First Programs Through Data Structures

Displaying Images Steps to create a label with an image: __init__ creates an instance of PhotoImage from a GIF file on disk The label’s image attribute is set to this object Fundamentals of Python: From First Programs Through Data Structures

Displaying Images (continued) The image label is placed in the grid before the text label The resulting labels are centered in a column in the window Fundamentals of Python: From First Programs Through Data Structures

Command Buttons and Responding to Events Fundamentals of Python: From First Programs Through Data Structures

Command Buttons and Responding to Events (continued) A button can display either text or an image To activate a button and enable it to respond to clicks, set command to an event-handling method In this case, _switch examines the text attribute of the label and sets it to the appropriate value Attributes are stored in a dictionary Fundamentals of Python: From First Programs Through Data Structures

Viewing the Images of Playing Cards Fundamentals of Python: From First Programs Through Data Structures

Entry Fields for the Input and Output of Text A form filler consists of labeled entry fields, which allow the user to enter and edit a single line of text A field can also contain text output by a program Tkinter’s Entry displays an entry field Three types of data container objects can be used with Entry fields: Fundamentals of Python: From First Programs Through Data Structures

Entry Fields for the Input and Output of Text (continued) Fundamentals of Python: From First Programs Through Data Structures

Using Pop-up Dialog Boxes Fundamentals of Python: From First Programs Through Data Structures

Using Pop-up Dialog Boxes (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: A GUI-Based ATM Request: Replace the terminal-based interface of the ATM program with a GUI Analysis: There are no new classes, although the ATM class now extends the Frame class Design: Instead of implementing a text-based, menu-driven command processor, ATM now implements a GUI-based, event-driven command processor _login and _logout Fundamentals of Python: From First Programs Through Data Structures

Case Study: A GUI-Based ATM (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: A GUI-Based ATM (continued) Implementation (Coding): … Fundamentals of Python: From First Programs Through Data Structures

Other Useful GUI Resources Layout of GUI components can be specified in more detail Groups of components can be nested in panes Paragraphs can be displayed in scrolling text boxes Lists of information can be presented for selection in scrolling list boxes and drop-down menus Color, size, and style of text and of some GUI components can be adjusted GUI-based programs can be configured to respond to various keyboard events and mouse events Fundamentals of Python: From First Programs Through Data Structures

Colors Tkinter module supports the RGB Values expressed in hex notation (e.g., #ff0000) Some commonly used colors have been defined as string values (e.g., "white", "black", "red") For most components, you can set two color attributes: A foreground color (fg) and a background color (bg) Fundamentals of Python: From First Programs Through Data Structures

Text Attributes The text displayed in a label, entry field, or button can also have a type font Fundamentals of Python: From First Programs Through Data Structures

Text Attributes (continued) Example: Fundamentals of Python: From First Programs Through Data Structures

Sizing and Justifying an Entry It’s common to restrict the data in a given entry field to a fixed length; for example: A nine-digit number for a Social Security number Fundamentals of Python: From First Programs Through Data Structures

Sizing the Main Window To set the window’s title: self.master.title(<a string>) Two other methods, geometry and resizable, can be run with the root window to affect its sizing self.master.geometry("200x100") self.master.resizable(0, 0) Generally, it is easiest for both the programmer and the user to manage a window that is not resizable Some flexibility might occasionally be warranted Fundamentals of Python: From First Programs Through Data Structures

Grid Attributes By default, a newly opened window shrink-wraps around its components and is resizable When window is resized, the components stay shrink-wrapped in their grid Grid remains centered within the window Widgets are also centered within their grid cells Occasionally, A widget must be aligned to left/right of its grid cell, Grid must expand with surrounding window, and/or Components must expand within their cells Fundamentals of Python: From First Programs Through Data Structures

Grid Attributes (continued) Fundamentals of Python: From First Programs Through Data Structures

Using Nested Frames to Organize Components Suppose a GUI requires a row of command buttons beneath two columns of labels and entry fields: It is difficult, but not impossible, to create this complex layout with a single grid Alternative: decompose window into two nested frames (panes), each containing its own grid Fundamentals of Python: From First Programs Through Data Structures

Using Nested Frames to Organize Components (continued) The new frame is then added to its parent’s grid and becomes the parent of the widgets in its own grid Fundamentals of Python: From First Programs Through Data Structures

Multi-Line Text Widgets Use Text widget to display multiple lines of text wrap attribute  CHAR (default), WORD, or NONE Widget can expand with its cell; alternative: scroll bars Text within a Text widget is accessed by index positions (specified as strings): "rowNumber.characterNumber" insert is used to send a string to a Text widget: output.insert("1.0", "Pythonƒrules!") output.insert(END, "Pythonƒrules! ") delete can be used to clear a Text widget: output.delete("1.0", END) Fundamentals of Python: From First Programs Through Data Structures

Multi-Line Text Widgets (continued) Fundamentals of Python: From First Programs Through Data Structures

Scrolling List Boxes Fundamentals of Python: From First Programs Through Data Structures

Scrolling List Boxes (continued) Fundamentals of Python: From First Programs Through Data Structures

Mouse Events Fundamentals of Python: From First Programs Through Data Structures

Mouse Events (continued) Associate a mouse event and an event-handling method with a widget by calling the bind method: self._theList.bind("<ButtonRelease-1>", self._get) Now all you have to do is define the _get method Method has a single parameter named event Fundamentals of Python: From First Programs Through Data Structures

Keyboard Events GUI-based programs can also respond to various keyboard events: Example: to bind the key press event to a handler self._radiusEntry.bind("<KeyPress-Return>", lambda event: self._area()) Fundamentals of Python: From First Programs Through Data Structures

Summary A GUI-based program responds to user events by running methods to perform various tasks The model/view/controller pattern assigns the roles and responsibilities to three different sets of classes Tkinter module includes classes, functions, and constants used in GUI programming A GUI-based program is structured as a main window class (extends the Frame class) Examples of window components: labels, entry fields, command buttons, text areas, and list boxes Fundamentals of Python: From First Programs Through Data Structures

Summary (continued) Pop-up dialog boxes display messages and ask yes/no question (use tkMessagebox class) Objects can be arranged using grids and panes Each component has attributes for the foreground color and background color Text has a type font attribute The command attribute of a button can be set to a method that handles a button click Mouse and keyboard events can be associated with handler methods for window objects (bind) Fundamentals of Python: From First Programs Through Data Structures