Reputation: 351
I am trying to build a graphical interface with python in GTK+/Pygobject, but i am having some trouble. Mainly with events.
What do i need? To execute a simple function whenever a button is clicked. Sample code:
class Window(Gtk.Window):
def __init__(self):
[...]
button = Gtk.Button()
icon = Gio.ThemedIcon(name="system-shutdown-symbolic")
image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON)
button.add(image)
button.clicked(self.on_button_clicked())
hb.pack_end(button)
[...]
def on_button_clicked(self):
print("Hello World")
Traceback:
Traceback (most recent call last): File "main.py", line 7, in executa = igrafica.Window() File "[...]/igrafica.py", line 23, in init button.clicked(self.on_button_clicked()) TypeError: clicked() takes exactly 1 argument (2 given)
It seemed quite obvious what button.clicked() should do, but its traceback talks about wrong numbers of arguments, and i can't find out what the problem is from this documentation i found. What am i doing wrong?
PS: Is there any official not "too-much-hardcore-for-newbies" documentation?
Upvotes: 0
Views: 426
Reputation: 57854
It does seem obvious what the clicked
signal should do, but you're misunderstanding the syntax for connecting a signal handler. Normally that would raise a more intuitive error, but in this case there's also a clicked()
method on Gtk.Button
that you are inadvertently calling. (That method is part of very old but not-yet-deprecated API, and fires a fake clicked
signal.)
Do this:
button.connect('clicked', self.on_button_clicked)
(remember not to put ()
after self.on_button_clicked
, as eduffy pointed out, because you're not calling the method, but passing it as a parameter to another method.)
Upvotes: 2
Reputation: 40224
button.clicked(self.on_button_clicked())
you are calling on_button_clicked
right here. remove the ()
to simply reference the method:
button.clicked(self.on_button_clicked)
Upvotes: 1