[Sugar-devel] gtk.button initial state on?

Art Hunkins abhunkin at uncg.edu
Fri Aug 26 22:39:34 EDT 2011


James,

I can't seem to get you very clear suggestions working here. While I'm 
having a difficult time getting the gray and green to show at the right 
times, the most important problem is that switching the self.buttons.append 
call from 0 to 1 doesn't effectively change the initial state.

I'd be much obliged if you could make me a proper gtk.ToggleButton method as 
a substitute. Initial state: on, color: green. Toggle to off, color: gray. 
Here's the "original" code below for your ready reference. (I presume the 
buttcallback method might remain as is?)

    def buttcallback(self, widget, data=None):
      for i in self.buttons:
         if i[0] == widget:
          if i[2]:
            i[2] = 0
            i[0].modify_bg(gtk.STATE_NORMAL, 
gtk.gdk.Color(0x8000,0x8000,0x8000, 1))
            i[0].modify_bg(gtk.STATE_PRELIGHT, 
gtk.gdk.Color(0x8000,0x8000,0x8000, 2))
          else:
            i[2] = 1
            i[0].modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(0,0x7700,0, 1))
            i[0].modify_bg(gtk.STATE_PRELIGHT, gtk.gdk.Color(0,0x7700,0, 2))
          self.set_channel(i[1], i[2])

    def button(self,box, title="",label=""):
       """Creates a button (on/off)
           box: parent box
           title: if given, the button name,
             which will also be the bus channel
             name. Otherwise a default name is
             given, BN, where N is button number
             in order of creation.
           label: if given, an alternative button name,
             which will be displayed instead of title
           returns the widget instance"""
       self.butts = self.butts + 1
       if title == "":
         title = "B%d" % self.butts
       if label == "": name = title
       else: name = label
       butt = gtk.Button(" %s " % name)
       self.scale_font(butt.child)
       butt.modify_bg(gtk.STATE_PRELIGHT, 
gtk.gdk.Color(0x8000,0x8000,0x8000, 2))
       box.pack_start(butt, False, False, 1)
       self.buttons.append([butt,title,0])
       butt.connect("clicked", self.buttcallback)
       butt.show()
       return butt

Art Hunkins

----- Original Message ----- 
From: "James Cameron" <quozl at laptop.org>
To: <sugar-devel at lists.sugarlabs.org>
Sent: Friday, August 26, 2011 8:11 PM
Subject: Re: [Sugar-devel] gtk.button initial state on?


> On Tue, Aug 23, 2011 at 03:00:10PM -0400, Art Hunkins wrote:
>> In my latest activity (SamplePlay), I've several normal buttons for
>> which I'd like the initial state to be *on*.
>
> The code is creating a gtk.Button (which has no persistent state) and
> then modifying the button background to indicate some sort of internal
> persistent state.
>
> (It should instead create a gtk.ToggleButton and use the set_active() and
> get_active() methods; it would be much easier to comprehend and change.
> But this code is written in a hurry using only the tools known to the
> programmer.)
>
>> Can someone show me how to modify the code below to achieve this? (The
>> off state is gray; the on state, green.)
>
> Sure.  It is the butt.modify_bg call when the button is created that is
> responsible for showing the gray.  It is the 0 in self.buttons.append
> that is responsible for setting the initial state off.
>
> (The programmer has taken control of how to show the button state).
>
> So you might change the butt.modify_bg call, and the self.buttons.append
> call.  Change the butt.modify_bg call by adopting the colour green, in
> the same way as you can see in buttcallback.  Change the
> self.buttons.append call by switching from 0 to 1 in the last list
> value.
>
> -- 
> James Cameron
> http://quozl.linux.org.au/
> _______________________________________________
> Sugar-devel mailing list
> Sugar-devel at lists.sugarlabs.org
> http://lists.sugarlabs.org/listinfo/sugar-devel 



More information about the Sugar-devel mailing list