Python DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Building GUI Applications with PythonCard and PyCrust
Pages: 1, 2, 3, 4

Resource Editing

The resource file for the Counter application appears below. While the structure of the resource file will be recognizable to any Python programmer, and can be edited by hand if necessary, it is typically maintained using the graphical Resource Editor tool supplied with PythonCard.




{'stack':{'type':'Stack', 
          'name':'Counter', 
    'backgrounds': [ 
    {'type':'Background', 
         'name':'bgcounter', 
          'title':'PythonCard Counter Tutorial', 
          'size':(204, 160), 
 
        'menubar': {'type':'MenuBar', 
         'menus': [ 
             {'type':'Menu', 
             'name':'menuFile', 
             'label':'&File', 
             'items': [ 
                  {'type':'MenuItem', 
                   'name':'menuFileExit', 
                   'label':'E&xit\tAlt+X', 
                  }, 
              ] 
             }, 
             {'type':'Menu', 
             'name':'menuCounter', 
             'label':'Counter', 
             'items': [ 
                  {'type':'MenuItem', 
                   'name':'menuCounterIncrement', 
                   'label':'Increment', 
                  }, 
                  {'type':'MenuItem', 
                   'name':'menuCounterDecrement', 
                   'label':'Decrement', 
                  }, 
                  {'type':'MenuItem', 
                   'name':'menuCounterReset', 
                   'label':'Reset', 
                  }, 
              ] 
             }, 
         ] 
     }, 
         'components': [ 
 
{'type':'Button',  
    'name':'resetBtn',  
    'position':(10, 68),  
    'label':'Reset',  
    }, 
 
{'type':'Button',  
    'name':'decrBtn',  
    'position':(10, 38),  
    'label':'Decrement',  
    }, 
 
{'type':'Button',  
    'name':'incrBtn',  
    'position':(10, 8),  
    'default':1,  
    'label':'Increment',  
    }, 
 
{'type':'TextField',  
    'name':'field1',  
    'position':(127, 19),  
    'size':(55, 46),  
    'editable':0,  
    'font':{'size': 24, 'family': 'sansSerif'},  
    'text':'42',  
    }, 
 
] # end components 
} # end background 
] # end backgrounds 
} } 

By separating PythonCard applications into two parts, the application code and the application resource file, we've removed some of the time and tedium involved in GUI development. At the same time, storing resource information as standard Python structures (dictionaries, lists, tuples, and so on) allows for the creation of additional editing tools or the manipulation of the resource file by custom utilities written in Python. Let's take a look at the Resource Editor and see how it works.

The Resource Editor is one of the development tools included with PythonCard, in addition to a Code Editor, Text Editor, and File Finder. The idea behind the Resource Editor is fairly simple--read and write to a resource file where GUI objects are described in general terms using standard Python structures. Inside the Editor, allow the creation and modification of GUI widgets and their properties. And automatically associate a resource file with an application so that the resource file is loaded and used to build the interface when the application is launched.

This figure shows the Resource Editor with the Counter application resource file loaded:

And here is another screen shot showing the Component menu options:

(In case you're curious, the answer is yes, the Resource Editor is itself a PythonCard application with its own resource file. In fact, the Resource Editor can be used to edit its very own resource file.)

Conclusion

The development of graphical user interfaces is a complex undertaking and we think we're headed in the right direction with PythonCard. Credit Alan Kay with originating the expression that "Simple things should be simple and complex things should be possible." We're doing our best to apply that motto to GUI development in Python. At the same time, there are plenty of areas that need to be explored. Recent discussions on the PythonCard list have centered on the use of sizers to control layout, the creation of compound components, and the ability to persist data transparently between application sessions. So we'd like to invite you to join in our efforts. Complete project information can be found at www.pythoncard.org. If you're attending OSCON 2002, plan to come to our tutorial . And, finally, we'd like to thank Python and wxPython for making our project possible.

Programming Python

Related Reading

Programming Python
Object-Oriented Scripting
By Mark Lutz

Patrick O'Brien is an independent software developer and trainer, specializing in the Python programming language. He is the creator of PyCrust, a developer on the PythonCard project, and leader of the PyPerSyst project. He may be reached at pobrien@orbtech.com.


Return to Python DevCenter.





Sponsored by: