Changing properties of a class instance widget

Martin Franklin MFranklin1 at gatwick.westerngeco.slb.com
Tue Jun 10 10:11:15 EDT 2003


Mark Light wrote:
> Hi,
>      I have a class (after some help from this newsgoup) that generates an
> array of buttons that I can select and deselect via left clicks etc(done
> using bind). What I would like to be able to do is to have a button that
> when clicked will select all in a particular coulmn of buttons. So I need to
> be able to referebce the separate instances.
> 
> A few snippets of the code!
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> #Class to generate sample buttons - including sample list population and
> data entry
> #CCC>
> class MyButton(Button):
>    ##<
>    def __init__(self, parent, buttonNb, **options):
>      self.__buttonNb = buttonNb
>      apply(Button.__init__, (self, parent), options)
>      self.configure( width=10)
>      self.bind('<Button-1>', self.onLeftClick)
>      self.bind('<Button-2>', self.onMiddleClick)
>      self.bind('<Button-3>', self.onRightClick)
>      self.bind('<Double-1>', self.onDoubleClick)
>    ##>
> 
>    #Define middleclick
>    ##<
>    def onMiddleClick(self, event):
>      self.config(None, bg=('green'), relief=SUNKEN)
>      print "MiddleClick on Sample" + `self.__buttonNb`
>    ##>
> 
> 
> #generate buttons from a list (called Layout - e.g. [(frame3b2, 1),
> (frame3b2, 2),....])
> for frame, sample in Layout:
>      b = MyButton(frame, sample, text="Sample %s" % sample)
>      b.pack(side=LEFT)
> 
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> How can I refer to a particular instance of MyButton to change it's widget
> properties.
> 
> 
> Many Thanks,
> 
> Mark.
> 

Hi Mark,


there are several ways to do this.  Perhaps the simplest is to store the 
button
instances in a python list as you create them.:-

myButtonInstanceList = []

for frame, sample in Layout:
     b = MyButton(frame, sample, text="Sample %s" % sample)
     b.pack(side=LEFT)
     myButtonInstanceList.append(b)



Then later you can do somthing like:-

thisButton = myButtonInstanceList[10]
thisButton.config(text="STOP!!!!")



HTH
Martin



















More information about the Python-list mailing list