[pygtk] Change state of a button

Casey McGinty casey.mcginty at gmail.com
Sat Aug 16 15:31:19 WST 2008


>
> On Fri, Jul 11, 2008 at 1:37 AM, Frédéric <[EMAIL PROTECTED]>
> wrote:
>
> >
> > I would like to see the corresponding button
> > pressed, as if I did it with the mouse.
> > <http://www.async.com.br/faq/pygtk/>
> >
>
>
> So you want to see the button go in and then out, like in real time? I don't
> think you can do this with a gtk.Button. Maybe you could rig a
> gtk.ToggleButton to emulate the button press. I would think that if you
> change the button state from active, to inactive, without adding any delay,
> it will happen too fast for you you to see it on the screen.
>
>
A while back I posted a response that I now realize was incorrect. You can
actually turn a gtk.Button into a gtk.ToggleButton quite easily. The
solution is to add event handlers to a gtk.Button that force it to behave
like a ToggleButton.
- Casey

Here is the solution:

#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk

def main():
   v = gtk.VBox()

   b = gtk.Button('Click to Toggle')
   b.set_border_width(5)

   def do_press(wdgt,event):
      # disable double/tripple clicks
      if event.type is not gtk.gdk.BUTTON_PRESS:
         return True
      if wdgt.state == gtk.STATE_ACTIVE:
         wdgt.do_button_release_event(wdgt,event)
      else:
         wdgt.do_button_press_event(wdgt,event)
         wdgt.emit('clicked')
      return True

   def do_click(wdgt):
      print 'click'

   b.connect('button-press-event',do_press)
   # disable the following to events from reaching the widget
   b.connect('button-release-event',lambda *arg:True)
   b.connect('leave-notify-event', lambda *arg:True)
   b.connect('clicked', do_click)

   v.pack_start(b)
   return v

w = gtk.Window( gtk.WINDOW_TOPLEVEL)
w.connect('destroy', gtk.main_quit)

w.add( main() )

w.show_all()
gtk.main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20080815/045998a6/attachment.htm 


More information about the pygtk mailing list