PC204 Lecture 9 Conrad Huang Genentech Hall, N453A x6-0415.

Slides:



Advertisements
Similar presentations
17 HTML, Scripting, and Interactivity Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and.
Advertisements

Introduction to Macromedia Director 8.5 – Lingo
Computer Science 112 Fundamentals of Programming II User Interfaces
Topics in Python Blackjack & TKinter
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
MSc. Publishing on WWW JavaScript. What is JavaScript? A scripting language devised by Netscape Adds functionality to web pages by: Embedding code into.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Understanding the Mainline Logical Flow Through a Program (continued)
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Introduction to R Statistical Software Anthony (Tony) R. Olsen USEPA ORD NHEERL Western Ecology Division Corvallis, OR (541)
1 ADVANCED MICROSOFT WORD Lesson 15 – Creating Forms and Working with Web Documents Microsoft Office 2003: Advanced.
Python quick start guide
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
Visual Basic Chapter 1 Mr. Wangler.
SAD and Python with EPICS at IHEP,Beijing, Noboru Yamamoto KEK, JAPAN.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Software Construction Lecture 10 Frameworks
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
CIM6400 CTNW (04/05) 1 CIM6400 CTNW Lesson 6 – More on Windows 2000.
An Introduction to Visual Basic
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.
IE 411/511: Visual Programming for Industrial Applications
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
Tutorial 8 Programming with ActionScript 3.0. XP Objectives Review the basics of ActionScript programming Compare ActionScript 2.0 and ActionScript 3.0.
CHAPTER TWO INTRODUCTION TO VISUAL BASIC © Prepared By: Razif Razali 1.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Dale Roberts Introduction to Visual Programming Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
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.
XP Tutorial 8 Adding Interactivity with ActionScript.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
9-Nov-97Tri-Ada '971 TASH An Alternative to the Windows API TRI-Ada ‘97 Terry J. Westley
Creating visual interfaces in python
HTML Forms.
EGR 115 Introduction to Computing for Engineers
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
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.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Visual Basic.NET Windows Programming
Development Environment
Topics Graphical User Interfaces Using the tkinter Module
PYGAME.
Introduction to Event-Driven Programming
Human Computer Interaction
Event loops 16-Jun-18.
Section 17.1 Section 17.2 Add an audio file using HTML
Predefined Dialog Boxes
Event loops.
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Computer Science 111 Fundamentals of Programming I User Interfaces
Topics Graphical User Interfaces Using the tkinter Module
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to AppInventor
Event loops 8-Apr-19.
Event loops.
Event loops.
Event loops 19-Aug-19.
Web Application Development Using PHP
Presentation transcript:

PC204 Lecture 9 Conrad Huang Genentech Hall, N453A x6-0415

Topics Homework review Interactive Programs vs Scripts Tkinter Pmw

Homework Review 8.1 – poker probabilities 8.2 – short project description

Interactive Program vs Script “Script” is a program where the programmer determines the order of execution from beginning to end – Programs may need user input and still be scripts, eg they may ask the user to type some input at a particular point of execution “Interactive program” is a program where the user determines the order of execution from user input using either: – a command-based text interface – a graphical user interface (GUI, pronounced goo-ey)

Interactive Program Characteristic The mark of an interactive program is an event dispatch loop For a text-based interactive program, it looks something like this for line in sys.stdin: args = line.strip().split() if not args: # skip blank lines continue if args[0] == “cmd1”: cmd1(args[1:]) elif args[0] == “cmd2”: cmd2(args[1:]) else: print “unknown command:”, args[0]

Event Loop and Event Dispatch The for loop reading user input is called an event loop because it waits for user events The event loop dispatches the user input to functions based on the input The event loop and dispatcher may be abstracted into a simple textual user interface module (tui.py)tui.py _registry = {} def mainloop(prompt="> "): import sys sys.stdout.write(prompt) while True: line = sys.stdin.readline() if not line: break args = line.strip().split() if not args: # ignore empty lines continue ambiguous = False f = None for k in _registry.keys(): if k.startswith(args[0]): if f is not None: ambiguous = True break f = _registry[k] if ambiguous: print ("Error: %s is an ambiguous command" % args[0]) elif f is None: print ("Error: %s is an unkown command" % args[0]) else: if not f(args[1:]): break sys.stdout.write(prompt) def register(function): _registry[function.__name__] = function

Using the TUI Module usetui.py is an example of how to use the TUI module usetui.py – Define functions that handle events – Register functions with TUI module – Enter the TUI module event loop tui module may be reused for many scripts def test(args): print "test", args return True def tilt(args): print "tilt", args return True def verify(args): print "verify", args return True def quit(args): print "quit" return False import tui tui.register(test) tui.register(tilt) tui.register(verify) tui.register(quit) tui.mainloop() print "Returned from command loop"

Libraries and Frameworks Library functions are called by user code to perform some tasks and return after the tasks are completed tui.mainloop() is somewhat different from most library functions in that it calls back to user- registered functions before returning A library that defines most functionality but allows callers to plug in their own code that gets called at predefined times is often called a framework

GUI Frameworks Most programs using a GUI framework follows the same form as our TUI program – Define callback functions – Define user interface elements, aka widgets, and register callback functions – Enter GUI event loop Implementation-wise, you need to design your user interface before you know what callback functions you need to make the program work

Tkinter Tkinter is a GUI framework Common widgets in Tkinter include: – Frame – placeholder for placement of widgets – Label – widget for displaying some text – Button – push button that user may press – Entry – input widget where user may type – Canvas – graphical drawing area A callback may be registered for a widget – eg Button callback is invoked when the button is pushed A function may also be bound to a widget to respond to events – eg Function gets called when user presses return in an entry widget

Basic Tkinter Widgets Using the Button widget – tkinter_button.py tkinter_button.py – tkinter_button2.py tkinter_button2.py – tkinter_button3.py tkinter_button3.py Using the Label and Entry widgets – tkinter_label.py tkinter_label.py – tkinter_label_entry.py tkinter_label_entry.py Controlling layout using Frame widgets – tkinter_label_entry2.py tkinter_label_entry2.py Binding actions to events – tkinter_label_entry3.py tkinter_label_entry3.py

Canvas and Text Displaying graphics on a canvas – tkinter_canvas.py tkinter_canvas.py A design pattern for a GUI class – tkinter_canvas2.py tkinter_canvas2.py Useful for homework assignment – tkinter_canvas3.py tkinter_canvas3.py Displaying and editing text – tkinter_text.py tkinter_text.py

More Tkinter Widgets Tkinter offers many more widgets – Some are simple, eg checkbutton and scale – Some are complex, eg scrollbars and menu buttons For more complete documentation on Tkinter widgets, see An Introduction to Tkinter by Fredrik Lundh –

More Complex Widgets Tkinter is a low-level interface that offers a lot of programming flexibility Libraries built on top of Tkinter offer more functionality with less programming – For example, managing scrollbars for canvas or text widgets is possible with Tkinter, but painful Python MegaWidgets (PMW) is layered on top of Tkinter and implements high(er)-level widgets such as ScrolledCanvas and NoteBook

Pmw Pmw ( is a Python package containing a number of “megawidgets” that are composed of basic Tkinter widgetshttp://pmw.sourceforge.net/ To use PMW, you need to download and install the Python code – Version 1.3 of PMW may be fetched locally – The simplest way to use this is to extract the “Pmw” folder and put it with your source code

An Aside on Packages A Python package is a folder of related Python modules – A Python package folder must contain a file named __init__.py, which tells Python the folder is not a random collection of.py files is executed when the package is imported – All other.py files in the folder are considered modules of the package and may be imported with import package_name.module_name Pmw uses some serious Python magic for its package management, so I usually just follow the recipes in the excellent documentationdocumentation

Pmw (cont.) pmw_scrolledcanvas.py – Uses canvas with attached scrollbars – Uses menu buttons for scrollbar modes – Uses button box for array of aligned buttons

Wrapping Up There are better ways for constructing GUIs – WYSIWYG interface design tools – More comprehensive widget sets Tkinter and Pmw together provide a usable set of tools for building graphical user interfaces A lot of applications are migrating to a client- server model that uses a web browser as a front end – GUI (client end) is written in HTML5 and Javascript – Server end is written in a variety of languages, including Python, PHP, Perl, …

Debugging Don’t guess – Collect data on where the error is occurring read the traceback add invariant, pre- and post-condition checks use print statements – Change one thing at a time Don’t get discouraged – No, the computer is not out to get you – Explain the code to someone (perhaps yourself) – Take a break don’t work until you can’t keep your eyes open

Homework and Project Assignment 9.1 – tkinter_canvas3.py should be helpful tkinter_canvas3.py Assignment 9.2 – A fuller description of your final project