Computer Science 112 Fundamentals of Programming II GUIs II:

Slides:



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

Chapter 8 Improving the User Interface
Tutorial 6 Creating a Web Form
Intermediate Level Course. Text Format The text styles, bold, italics, underlining, superscript and subscript, can be easily added to selected text. Text.
Chapter 7 Improving the User Interface
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
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.
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.
HTML Concepts and Techniques Fourth Edition Project 7 Creating a Form on a Web Page.
Python, Toolboxes, Tools & Script Tools
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
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)
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
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.
Copyright © Curt Hill More Components Varying the input of Dev-C++ Windows Programs.
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.
Basic HTML.
Guide to Programming with Python
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 3 Building an Application in the Visual Basic.NET Environment.
Working with the Window Object JavaScript considers the browser window an object, which it calls the window object.
Tutorial 6 Creating a Web Form
See Winter 2016CISC101 - Prof. McLeod1.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Computer Science 112 Fundamentals of Programming II Data Fields for I/O and Message Boxes for Error Recovery.
GUI’s.
Multimedia Summer Camp
Excel Tutorial 8 Developing an Excel Application
Fundamentals of Programming I Overview of Programming
Topics Graphical User Interfaces Using the tkinter Module
GUI Final Project: MPEG Maker
Graphical User Interfaces (GUIs)
Computer Science 111 Fundamentals of Programming I
Chapter 2 – Introduction to the Visual Studio .NET IDE
An Introduction to Computers and Visual Basic
Section 64 – Manipulating Data Using Methods – Java Swing
My Final Project: Calorie Counter
Fundamentals of Programming I The Model/View/Controller Design Pattern
Chap 7. Building Java Graphical User Interfaces
CISC101 Reminders Grading of Quiz 4 underway.
Exceptions and files Taken from notes by Dr. Neil Moore
Graphical User Interfaces -- Introduction
Fundamentals of Python: From First Programs Through Data Structures
Objectives Explore web forms Work with form servers
Creating a Workbook Part 2
Tkinter GUIs Computer Science and Software Engineering
Variables and Arithmetic Operations
Fundamentals of Python: First Programs Second Edition
Topics Introduction to File Input and Output
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
Exceptions and files Taken from notes by Dr. Neil Moore
Fundamentals of Programming I Windows, Labels, and Command Buttons
Computer Science 111 Fundamentals of Programming I Text Areas
Computer Science 111 Fundamentals of Programming I User Interfaces
Additional Topics in VB.NET
Fundamentals of Programming I The Model/View/Controller Design Pattern
Topics Graphical User Interfaces Using the tkinter Module
Computer Science 111 Fundamentals of Programming I
DAT2343 LMC Simulator Usage © Alan T. Pinck / Algonquin College; 2003.
Chapter 7 Event-Driven Pages
Topics Introduction to File Input and Output
UI Elements 2.
Presentation transcript:

Computer Science 112 Fundamentals of Programming II GUIs II: Organizing Widgets with Panels Data Fields for I/O Message Boxes for Error Recovery

One Background or Two?

Problems If all the widgets go directly in the window, they must have only one common background They’re all in the same grid, but we might want different subgrids for different groups of widgets in the same window

Solution: Use Panels A panel is a blank rectangular area within a window A panel is added to the window’s grid, like other widgets Other widgets, including other panels, are then added to the panel

class FrenchFlag(EasyFrame): """Displays the French flag in the window.""" def __init__(self): """Sets up the window and the panels.""" EasyFrame.__init__(self, title = "French Flag", width = 300, height = 200) self.addPanel(0, 0, background = "blue") self.addPanel(0, 1, background = "white") self.addPanel(0, 2, background = "red"))

In CounterGUI: def __init__(self, counter): """Sets up the window, label, and buttons.""" EasyFrame.__init__(self, title = "Counter", background = "lightblue") self.counter = counter self.label = self.addLabel(text = str(self.counter), row = 0, column = 0, sticky = "NSEW", columnspan = 3, buttonPanel = self.addPanel(1, 0, background = "pink") buttonPanel.addButton(text = "Decrement", command = self.decrement) buttonPanel.addButton(text = "Reset", row = 0, column = 1, command = self.reset) buttonPanel.addButton(text = "Increment", row = 0, column = 2, command = self.increment)

About Panels Each panel has its own grid, independent of the enclosing window or panel’s grid Any type of widget that can be placed in a window can be placed in a panel Panels can be nested to any depth

A Mythical Tax Code All incomes are taxed at a single flat rate, say, 15% Each family member receives an exemption amount, say, $3500 tax = max(0.0, (income - exemptions * 3500) * 0.15) Inputs: income and number of exemptions Output: tax

TUI and Code print("Tax Calculator\n") RATE = .15 EXEMPTION_AMOUNT = 3500.0 income = float(input("Please enter your income: $")) exemptions = int(input("Please enter the number of exemptions: ")) tax = max(0.0, (income - exemptions * EXEMPTION_AMOUNT) * RATE) print("Your total tax is $%0.2f" % tax)

TUI vs GUI

Data Fields The IntegerField class is used for the I/O of integers only The FloatField class is used for the I/O of integers or floats Use addIntegerField and addFloatField, respectively

Attributes and Methods Field attributes value (the field’s initial numeric value) state (“normal” or “readonly”) width (20 columns for float fields, 10 for integer fields) precision (for float fields only, infinite by default) Field methods getNumber() returns the field’s value as a number setNumber(anIntOrFloat) resets the field’s value setPrecision(anInt) for float fields only

class TaxCode(EasyFrame): """Application window for the tax calculator.""" RATE = .15 EXEMPTION_AMOUNT = 3500.0 def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self, title = "Tax Calculator") # Label and field for the income self.addLabel(text = "Income", row = 0, column = 0) self.incomeField = self.addFloatField(value = 0.0, row = 0, column = 1) # Label and field for the number of exemptions self.addLabel(text = "Exemptions", row = 1, column = 0) self.exField = self.addIntegerField(value = 0, row = 1,

class TaxCode(EasyFrame): """Application window for the tax calculator.""" RATE = .15 EXEMPTION_AMOUNT = 3500.0 def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self, title = "Tax Calculator") . . . # The command button self.button = self.addButton(text = "Compute", row = 2, column = 0, columnspan = 2, command = self.computeTax) # Label and field for the tax self.addLabel(text = "Total tax", row = 3, column = 0) self.taxField = self.addFloatField(value = 0.0, row = 3, column = 1, precision = 2, state = "readonly")

class TaxCode(EasyFrame): """Application window for the tax calculator.""" RATE = .15 EXEMPTION_AMOUNT = 3500.0 def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self, title = "Tax Calculator") . . . # The event handling method for the button def computeTax(self): """Obtains the data from the input fields and uses them to compute the tax, which is sent to the output field.""" income = self.incomeField.getNumber() exemptions = self.exField.getNumber() tax = max(0.0, (income - exemptions * TaxCode.EXEMPTION_AMOUNT) \ * TaxCode.RATE) self.taxField.setNumber(tax)

Input Errors: Number Out of Range

Error Recovery Check for inputs out of range Pop up a message box with a descriptive error message Continue to wait for new inputs

def computeTax(self): """Obtains the data from the input fields and uses them to compute the tax, which is sent to the output field. Responds with error message if inputs are invalid.""" income = self.incomeField.getNumber() exemptions = self.exField.getNumber() if income < 0 or exemptions < 0: self.messageBox("ERROR ALERT", "Income and number of exemptions must be >= 0") return tax = max(0.0, (income - exemptions * TaxCode.EXEMPTION_AMOUNT) \ * TaxCode.RATE) self.taxField.setNumber(tax)

Input Errors: Bad Digits

Trapping Exceptions Must use Python’s try/except statement When exception is trapped, pop up a message box with an error message try: <code that might raise an exception> except <exception type>: <recovery code>

Trapping an Exception def computeTax(self): """Obtains the data from the input fields and uses them to compute the tax, which is sent to the output field. Responds with error message if inputs are invalid.""" try: income = self.incomeField.getNumber() exemptions = self.exField.getNumber() if income < 0 or exemptions < 0: self.messageBox("ERROR ALERT", "Income and number of exemptions must be >= 0") return tax = max(0.0, (income - exemptions * TaxCode.EXEMPTION_AMOUNT) \ * TaxCode.RATE) self.taxField.setNumber(tax) except ValueError: self.messageBox("ERROR ALERT", "Inputs must be numbers")

Widgets for Text I/O A text field (class TextField) is used for the I/O of one-line strings A text area (class TextArea) is used for the I/O of multiple lines of text See the breezypythongui site for details

Example: A Simple Text File Editor Shows a text area to view and edit the contents of a text file Includes command buttons for opening and saving a file

App Window at Startup

Pop up an Open File Dialog

Load File Contents into Text Area

from breezypythongui import EasyFrame import tkinter.filedialog class FileDialogDemo(EasyFrame): """Demonstrates file dialogs and a text area.""" def __init__(self): """Sets up the window and widgets.""" EasyFrame.__init__(self, "File Dialog Demo") self.outputArea = self.addTextArea("", row = 0, column = 0, columnspan = 2, width = 80, height = 15) self.addButton(text = "Open", row = 1, column = 0, command = self.openFile) self.addButton(text = "Save As...", row = 1, column = 1, command = self.saveFileAs)

from breezypythongui import EasyFrame import tkinter.filedialog class FileDialogDemo(EasyFrame): """Demonstrates file dialogs and a text area.""" . . . # Event handling methods def openFile(self): """Pops up an open file dialog, and if a file is selected, displays its text in the text area.""" filetypes = [("Python files", "*.py"), ("Text files", "*.txt")] fileName = tkinter.filedialog.askopenfilename(parent = self, filetypes = filetypes) if fileName != "": file = open(fileName, "r") text = file.read() file.close() self.outputArea.setText(text) self.setTitle(fileName) def saveFileAs(self): """Pops up a save file dialog, and if a file is selected, saves the contents of the text area to the file.""" fileName = tkinter.filedialog.asksaveasfilename(parent = self) text = self.outputArea.getText() file = open(fileName, "w") file.write(text)

Scrolling list boxes Popup dialogs For Wednesday Scrolling list boxes Popup dialogs