Computer Science 112 Fundamentals of Programming II Data Fields for I/O and Message Boxes for Error Recovery.

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
Creating a Form on a Web Page
Supplement Creating Forms. Objectives Show how forms are used How to create the Form element HTML elements used for creating input fields.
Intermediate Level Course. Text Format The text styles, bold, italics, underlining, superscript and subscript, can be easily added to selected text. Text.
Access - Project 1 l What Is a Database? –A Collection of Data –Organized in a manner to allow: »Access »Retrieval »Use of That Data.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
CASE STUDY: Applet Development with GUI and Client-side File Input Arkadiusz Edward Komenda.
Chapter 7 Improving the User Interface
Graphical User Interfaces A Quick Outlook. Interface Many methods to create and “interface” with the user 2 most common interface methods: – Console –
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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.
Introduction to Matlab & Data Analysis
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Introduction. 2COMPSCI Computer Science Fundamentals.
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.
COSC 1P02 Introduction to Computer Science 9.1 Cosc 1P02 "A lie gets halfway around the world before the truth has a chance to get its pants on.” Sir Winston.
Copyright © Curt Hill First Window Builder Program Easy GUIs in Eclipse.
Exploring Office Grauer and Barber 1 Committed to Shaping the Next Generation of IT Experts. Chapter 2 – Gaining Proficiency: The Web and Business.
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.
Chapter Two Creating a First Project in Visual Basic.
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.
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.
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.
HTML Forms. Slide 2 Forms (Introduction) The purpose of input forms Organizing forms with a and Using different element types to get user input A brief.
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.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
Tutorial 6 Creating a Web Form
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
This screen may be skipped altogether if the user chooses a report from the server and clicks Ad Hoc or Edit or whatever. Also, the next screen would ordinarily.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
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
Computer Science 111 Fundamentals of Programming I
An Introduction to Computers and Visual Basic
Section 64 – Manipulating Data Using Methods – Java Swing
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
Fundamentals of Python: First Programs Second Edition
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
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
UI Elements 2.
Presentation transcript:

Computer Science 112 Fundamentals of Programming II Data Fields for I/O and Message Boxes for Error Recovery

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 = 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 and 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 = 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, column = 1)

class TaxCode(EasyFrame): """Application window for the tax calculator.""" RATE =.15 EXEMPTION_AMOUNT = 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 = 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: except :

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") Trapping an Exception

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) if fileName != "": text = self.outputArea.getText() file = open(fileName, "w") file.write(text) file.close() self.setTitle(fileName)

For Monday (actually, Tuesday) Graphics and Mouse Events