Class for custom Tkinter widget--difficulty

Kevin Walzer kw at codebykevin.com
Mon Sep 17 16:11:03 EDT 2007


I'm trying to create a custom Tkinter widget class, and I'm having some 
difficulty getting it set up properly.

The class is called MacToolbar, saved in its own module MacToolbar.py, 
and imported with this statement:

	import MacToolbar

Here is the relevant portion of the class:

###relevant class code

class MacToolbar:
     def __init__(self, master):
         self.tk.call("package", "require", "macsearchfield")
         self.tk.call('package', 'require', 'tile')


     def create(self):
         self.baseframe = Tile.Frame(self, master)
         self.baseframe.pack(side='top', fill='both', expand='no')


     def toolbar(self):
         self.create()
         self.newframe = Tile.Frame(self.baseframe)
         self.newframe.pack(fill='both', expand='yes')

         self.buttonframe = Tile.Frame(self.newframe)
         self.buttonframe.pack(side='top', fill='both', expand = 'yes')


I'm a bit confused on how to call a class that is imported via a module. 
Both approaches I try produce errors:

The first approach:

	self.topframe = MacToolbar.toolbar(self.mainframe)

yields this error:

	AttributeError: 'module' object has no attribute 'toolbar'

The second approach:

  	self.topframe = MacToolbar.MacToolbar.toolbar(self.mainframe)

yields this error:

	TypeError: unbound method toolbar() must be called with MacToolbar 
instance as first argument (got Frame instance instead)

I was expecting to be able to use the standard Class.Method notation in 
calling MacToolbar class methods, but it appears that this does not work 
at all: that's what I see in the first error above. Instead I have to 
use Module.Class.Method notation? I've never seen this before.

Can someone point out what I am doing wrong, either in the construction 
of the class, the way it's imported, or in how I'm calling methods?

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com



More information about the Python-list mailing list