1

I'm writing this application using quickly.

I'm looking for a way to toggle button without running function which is connected to this button.

def on_button_text_italic_toggled(self, widget):
    print "Italic"

def on_buttone_test_clicked(self, widget):
    self.button_text_italic.set_active(True)

So I need this to make button_text_italic to be toggled but without printing out "Italic" text.

Thanks for any help!

2 Answers 2

4

If you want the function to be run most of the time the toggle signal is emitted, and not when you manually toggle it (e.g. when loading saved settings and displaying the appropriate state), then you need to block and unblock the signal. To do so you'll need the handle_id that was returned when the signal was connected to the function. Just assign a variable when you connect the signal. Here's an example:

#!/usr/bin/python
from gi.repository import Gtk

def on_toggle(widget,data=None):
    print "toggled, emitted signal"

def on_button1_clicked(widget, data=None):
    print "manually toggle, no signal"
    toggle.handler_block(handle_id)
    state=toggle.get_active()
    toggle.set_active(not state)
    toggle.handler_unblock(handle_id)


win=Gtk.Window()
win.connect('destroy', Gtk.main_quit)
box=Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
button1=Gtk.Button('Toggle with no signal')
button1.connect('clicked', on_button1_clicked)
button1.show()
box.pack_start(button1,True,True,10)
toggle=Gtk.ToggleButton('Toggle')
handle_id=toggle.connect('toggled', on_toggle)
toggle.show()
box.pack_start(toggle,True,True,0)
box.show_all()
win.add(box)
win.show()
Gtk.main()
2
  • as always you saved me! Thank you so much! I've heard about handler_block before but didn't know how to implement it, now I know! thanks once again.
    – Majster-pl
    Apr 12, 2013 at 15:12
  • 1
    If you create your interface and connect the signals with Glade, you don't have the id laying around. In that case you can use handler_block_by_func which does the same, but requires a function instead of id.
    – Timo
    Apr 12, 2013 at 15:49
0

on_button_text_italic_toggled is an object method which gets invoked when the on_buttone_test_clicked gets called so it is natural to execute it's specified code.

You will have to replace the print "Itallic" line with pass a Python keyword but this leaves you with the on_button_text_italic_toggled doing nothing so you may not want to invoke it.

You can learn more about functions here and how to define classes and methods here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .