problem in the compiler ?

Scott David Daniels Scott.Daniels at Acm.Org
Tue May 3 15:28:03 EDT 2005


Glauco Silva wrote:
> ... so, my code is this:
> file : MyClass.py
>     import Pmw
>     class MyClass:
>         def __init__(self):
>             radio = Pmw.RadioSelect()
>             radio.bind('<ButtonRelease>', self.Function)
>             #Pmw.RadioSelect(command = self.Function)
> 
>         def Funciton(self):
>             print "Enter in the Function"
> 
>     if __name__ == '__main__' :
>         MyClass()

But, when I make such a file, I get:

Traceback (most recent call last):
   File "MyClass.py", line 12, in ?
     MyClass()
   File "MyClass.py", line 5, in __init__
     radio.bind('<ButtonRelease>', self.Function)
AttributeError: MyClass instance has no attribute 'Function'

Cut-paste is better than typing what you mean.  When I correct
the spelling of "Funciton," I get no output at all.
If I do:
     >>> import Pmw
     >>> Pmw.version()
I get:
     '1.2'

I merged your supposed code with the Pmw documentation's page on
the RadioSelect class:
	http://pmw.sourceforge.net/doc/RadioSelect.html
  I reformatted a bit and commented with ## a couple of things I suspect
you have in your unrevealed code:

----------------------
import Pmw
class MyClass:
     def __init__(self):
         radio = Pmw.RadioSelect()
         radio.bind('<ButtonRelease>', self.Function)
         Pmw.RadioSelect(command=self.Function)

     def Function(self):
         print "Enter in the Function"

class Demo:
     def __init__(self, parent):
         # Create and pack a horizontal RadioSelect widget.
         horiz = Pmw.RadioSelect(parent, labelpos='w',
                     command=self.callback, label_text='Horizontal',
                     frame_borderwidth=2, frame_relief='ridge')
         horiz.pack(fill='x', padx=10, pady=10)

         # Add some buttons to the horizontal RadioSelect.
         for text in ('Fruit', 'Vegetables', 'Cereals', 'Legumes'):
             horiz.add(text)
         horiz.invoke('Cereals')  ## this fires off the Cereals button

         # Create and pack a multiple selection RadioSelect widget.
         self.multiple = Pmw.RadioSelect(parent, labelpos='w',
             command=self.multcallback, label_text='Multiple\nselection',
             frame_borderwidth=2, frame_relief='ridge',
             selectmode='multiple')
         self.multiple.pack(fill='x', padx=10)

         # Add some buttons to the multiple selection RadioSelect.
         for text in ('Apricots', 'Eggplant', 'Rice', 'Lentils'):
             self.multiple.add(text)
         self.multiple.invoke('Rice') ## this fires off the Rice button

         # Make & pack a vertical RadioSelect widget, with checkbuttons.
         self.checkbuttons=Pmw.RadioSelect(parent, labelpos='w',
             buttontype='checkbutton',
             orient='vertical', command=self.checkbuttoncallback,
             label_text='Vertical,\nusing\ncheckbuttons',
             hull_borderwidth=2, hull_relief='ridge')
         self.checkbuttons.pack(side='left', expand=1, padx=10, pady=10)

         # Add some buttons to the checkbutton RadioSelect.
         for text in ('Male', 'Female'):
             self.checkbuttons.add(text)
         self.checkbuttons.invoke('Male') ## fires the Male button
         self.checkbuttons.invoke('Female') ## fires the Female button

         # Create and pack a RadioSelect widget, with radiobuttons.
         radiobuttons = Pmw.RadioSelect(parent, buttontype='radiobutton',
             orient='vertical', labelpos='w', command=self.callback,
             label_text='Vertical,\nusing\nradiobuttons',
             hull_borderwidth=2, hull_relief='ridge')
         radiobuttons.pack(side='left', expand=1, padx=10, pady=10)

         # Add some buttons to the radiobutton RadioSelect.
         for text in ('Male', 'Female', 'Both', 'Neither'):
             radiobuttons.add(text)
         radiobuttons.invoke('Both')  ## this fires off the Both button

     def callback(self, tag):
         # This is called whenever the user clicks on a button
         # in a single select RadioSelect widget.
         print 'Button', tag, 'was pressed.'

     def multcallback(self, tag, state):
         # This is called whenever the user clicks on a button
         # in the multiple select RadioSelect widget.
         if state:
            print 'mButton', tag, 'was pressed.',
         else:
            print 'mButton', tag, 'was released.',
         print 'Selection:', self.multiple.getcurselection()

     def checkbuttoncallback(self, tag, state):
         # This is called whenever the user clicks on a button
         # in the checkbutton RadioSelect widget.
         if state:
            print 'cButton', tag, 'was pressed.',
         else:
            print 'cButton', tag, 'was released.',
         print 'Selection:', self.checkbuttons.getcurselection()

if __name__ == '__main__' :
     MyClass()
     print 'MyClass constructed'
     Demo(None)
     print 'Demo constructed'

----------------
When I run it, I get:

     MyClass constructed
     Button Cereals was pressed.
     mButton Rice was pressed. Selection: ('Rice',)
     cButton Male was pressed. Selection: ('Male',)
     cButton Female was pressed. Selection: ('Male', 'Female')
     Button Both was pressed.
     Demo constructed

I suspect you are either not running the code you think you are running,
or you are not really telling us what code you are running.  You are
trying to simplify for exposition, and in the process hiding the issue.

By the way, none of this code runs the Tkinter mainloop (which would
probably be a good idea).  I would suggest running something like this 
at the end:

if __name__ == '__main__' :
     import Tkinter
     # MyClass()
     # print 'MyClass constructed'
     Demo(None)
     print 'Demo constructed'
     Tkinter.mainloop()


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list