PythonCard notes for Mac: Running the Layout Editor: it does not run - - PDF document

pythoncard notes for mac
SMART_READER_LITE
LIVE PREVIEW

PythonCard notes for Mac: Running the Layout Editor: it does not run - - PDF document

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


slide-1
SLIDE 1

1

1

Aaron Stevens

15 April 2009

CS108 Lecture 30: Graphical User Interface: Selection using PythonCard

2

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.

slide-2
SLIDE 2

2

3

Overview/Questions

– Review using Layout Editor.

  • button, events, text input

– How do we collect choices from the user?

4

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)

slide-3
SLIDE 3

3

5

Example

Let’s build the following example window:

6

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

slide-4
SLIDE 4

4

7

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.

8

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):"

slide-5
SLIDE 5

5

9

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

10

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>

slide-6
SLIDE 6

6

11

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.

12

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

slide-7
SLIDE 7

7

13

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

14

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

slide-8
SLIDE 8

8

15

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

  • bjects 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

16

What are the widgets’ attributes?

You can then call this method from the

  • n_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

slide-9
SLIDE 9

9

17

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.

18

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