CS108 Lecture 30: Graphical User Interface: Selection using PythonCard Aaron Stevens 15 April 2009 1 PythonCard notes for Mac: Running the Layout Editor: it does not run from IDLE. You can run it from the command line. I have a script I can give you/help you set up. I have a fix to layoutEditor.py that deals with resizing the window. 2 1
Overview/Questions – Review using Layout Editor. button, events, text input – How do we collect choices from the user? 3 Adding Widgets Let’s add some widgets to the layout: – Check Box (a binary choice) – Radio Buttons (a multiple choice) – Combo Box (a multiple choice) 4 2
Example Let’s build the following example window: 5 Naming Widgets Widget Prefixes: –rad for a Radio Button Group –chk for Check box –cho for a Choice box This webpage has a list of naming conventions to follow: http://www.cs.bu.edu/courses/cs108/gui_naming_conventions.html 6 3
Adding Event Handlers PythonCard event handlers are declared as methods on a class, and must follow this exact naming convention: def on_<component-name>_<event>(self, event): where <component-name> is the name you gave the component in Layout Editor, and <event> is the event name. 7 Event Handlers We can use either a Button’s mouseClick event, or else use a widget’s mouseClick event. Example: def on_chkSugar_mouseClick(self, event): print "def on_chkSugar_mouseClick(self, event):" 8 4
Adding Event Handlers About 20 events are supported. The most common ones are: –command –mouseClick –initialize –select The complete list is posted here: http://pythoncard.sourceforge.net/framework/events_and_handlers.html 9 Interacting with the Widgets Each of the widgets has some attributes, which we will want to interact with from our code. We will use this general form: self.components.<widget-name>.<attribute> 10 5
Check Box Attributes The CheckBox ’s most useful attribute is: – checked , which is a Boolean for whether the user checked this box. Applying this general form: self.components.<widget-name>.<attribute> We would use: self.components.chkSugar.checked Which will yield either True or False , which we can use in some decision making. 11 Radio Group Attributes The RadioGroup ’s most useful attributes are: – selection , which is the numeric index of the element which was picked. – items , which is the list from which the user picked. self.components.<component-name>.selection self.components.<component-name>.items 12 6
Choice Box Attributes The Choice ’s most useful attributes are: – selection , which is the numeric index of the element which was picked. – items , which is the list from which the user picked. self.components.<component-name>.selection self.components.<component-name>.items 13 What are the widgets’ properties? Each of the widgets has some properties, which we will want to interact with from our code. Go to the API pages, here: http://pythoncard.sourceforge.net/framework/components.html 14 7
What are the widgets’ attributes? Another trick: – You can find out what the components are called, and what they can do, using introspection – asking the objects what their attributes are: def debugComponentsAndAttributes(self): for component in self.components.iterkeys(): # self.components is a dictionary (and also an object) print component, "->", type(self.components[component]) attributes = dir(type(self.components[component])) for attr in attributes: if attr[0] >= 'a': # only print lower case names print "\t",attr print 15 What are the widgets’ attributes? You can then call this method from the on_initialize(...) event, and at startup time it will print out: – the names of each of the components – the class which defines each component’s attributes – the list of attributes we care about for each 16 8
Take-Away Points – Use Layout Editor to specify selection widgets. – Use Property Editor to modify the widgets’ properties (name, size, location, list of items.) – Write event handler methods in the class definition. – Interact with widgets through their properties. 17 Student To Dos – Readings: Using the PythonCard Layout editor http://pythoncard.sourceforge.net/walkthrough2.html – HW13 due TUESDAY 4/21 – Final HW due THURSDAY 4/30 @ Noon bonus if you’re done/can show it to me MON 4/27 18 9
Recommend
More recommend