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

Benjamin Berg benzea at sugarlabs.org
Sat Aug 27 05:36:50 EDT 2011


Hello,

As James already suggested, I would say you really should use a
gtk.ToggleButton. This button correctly stores the state itself.

Now, if you really want to modify the background to green for the active
state, then you can still do this by just calling
  button.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.Color(...))
once.

After that you just connect to the "toggled" signal, and read out the
current state with button.get_active() in the signal handler.

Benjamin

PS: May I also suggest passing the title trough to the toggle handler,
or attaching it to the button. This should simplify the callback handler
a lot:

def on_button_toggled(self, widget, title):
    self.set_channel(title, widget.get_active())

and the connect call becomes:

    butt.connect("clicked", self.on_button-toggled, title)

On Tue, 2011-08-23 at 15:00 -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*. Can someone show me how to modify the 
> code below to achieve this? (The off state is gray; the on state, green.)
> 
> The code works fine except for the initial state (by default, off). (The 
> code is from Victor Lazzarini's csndsugui.py, a gtk gui toolkit for 
> Sugar/Csound.)
> 
>     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, 2))
>             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





More information about the Sugar-devel mailing list