(GTK3) Gtk.Box.add() called from Gtk.Button clicked event does nothing
In the following example app:
import gi
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GLib, Gtk, Gdk, GObject
class ButtonBox(Gtk.Box):
"""Button container"""
counter = 0
def __init__(self):
super().__init__()
print('ButtonBox created, I am ' + str(self) + '\n')
self.add_to_self(called_by='the ButtonBox init function')
def add_to_self(self, called_by=None):
"""Adds a button to the widget."""
print('I was called by ' + str(called_by))
print('And I am a ' + str(self))
self.counter += 1
button = Gtk.Button(label=str(self.counter))
print('New button: ' + str(button) + '\n')
self.add(button)
class Application(Gtk.Window):
"""Demo app"""
def __init__(self):
super().__init__(title="Demo app")
container = Gtk.Box()
self.add(container)
buttonbox = ButtonBox()
container.add(buttonbox)
specialbutton = Gtk.Button(label='Click me to add a button!')
specialbutton.connect('clicked', buttonbox.add_to_self)
container.add(specialbutton)
def quit(self):
Gtk.main_quit()
def app_main():
win = Application()
win.connect("destroy", Application.quit)
win.show_all()
if __name__ == "__main__":
app_main()
Gtk.main()
The function buttonbox.add_to_self
is called (as evidenced by the print statement showing up), but the new button is never added.
I tried replacing
specialbutton.connect('clicked', widget.add_to_self)
with
specialbutton.connect('clicked', lambda x: widget.add_to_self())
but nothing changed.
(If I'm making this issue in the wrong repository, let me know. I'm still getting started with pygobject, and I'm not entirely sure which library is responsible for what - to me, it seems like it could be a pygobject issue, but it could be a GTK issue as well.)
Edited by knuxify