Python 3.x 如何从Gtk 3窗口动态添加/删除小部件

Python 3.x 如何从Gtk 3窗口动态添加/删除小部件,python-3.x,gtk3,pygobject,Python 3.x,Gtk3,Pygobject,我将python3与Gtk3一起使用,我需要基本上从我的Gtk.Window中删除一些小部件,并在单击Gtk.Button时用其他小部件替换它们。我已经尝试将Gtk.ListBox与Gtk.ListBoxRow一起使用es,到目前为止,我可以从列表框但是当我尝试将它们添加回来时,基本上什么都没有发生。这是我代码的一部分: def left_view(self, box): listbox = box row0 = Gtk.ListBoxRow() row0.set_ha

我将python3与Gtk3一起使用,我需要基本上从我的
Gtk.Window
中删除
一些小部件,并在单击
Gtk.Button
时用其他小部件替换它们。我已经尝试将
Gtk.ListBox
Gtk.ListBoxRow
一起使用
es,到目前为止,我可以从
列表框
但是当我尝试将它们添加回来时,基本上什么都没有发生。这是我代码的一部分:

def left_view(self, box):
    listbox = box

    row0 = Gtk.ListBoxRow()
    row0.set_halign(Gtk.Align.START)
    listbox.add(row0)
    #Adding HorizontalBox to place widgets
    hbox = Gtk.Box(spacing=10,orientation=Gtk.Orientation.HORIZONTAL)
    row0.add(hbox)
    prod_name = Gtk.Label()
    stock_count = Gtk.Label()
    prod_name.set_text('Product Name')
    stock_count.set_text('Stock   ')
    self.prod_name_input = Gtk.Entry()
    self.stock_count_input = Gtk.Entry()

    #Packaging 101
    hbox.pack_start(prod_name, True, True, 0)
    hbox.pack_start(self.prod_name_input, True, True, 0)
    hbox.pack_start(stock_count, True, True, 0)
    hbox.pack_start(self.stock_count_input, True, True, 0)
函数将被赋予一个
Gtk.ListBox
作为参数。到目前为止,我在尝试
remove()
add()
将内容添加到ListBox时得到的是:

def remover(self,widget):
        vbox = widget.get_parent()
        base = vbox.get_parent()
        children = base.get_children()
        listbox = children[1]
        for row in listbox.get_children():
            listbox.remove(row)
        with open('product_info.json', 'r') as d:
            data = d.read()
            j = json.loads(data)
            d.close()
        self.keys = list(j.keys())
        row0 = Gtk.ListBoxRow()
        listbox.add_child(row0)
        scrollwindow = Gtk.ScrolledWindow()
        scrollwindow.set_hexpand(True)
        scrollwindow.set_vexpand(True)
        row0.add_child(scrollwindow)
        tview = Gtk.TextView()
        scrollwindow.add(tview)
        textbuffer = tview.get_buffer()
        for item in range(0, len(self.keys)):
            textbuffer.insert_at_cursor('   %s\t\t %s \n' %(item, self.keys[item]))

顺便说一句,如果有更简单的方法在PyGObject中随时更换小部件,请务必让我知道,因为我在相关问题上看到的大多数答案都是毫无用处的,我对此也感到困惑。事实证明,创建的小部件必须使用
show()
show\u all()
显式显示。就你而言:

    row0.show_all()

作为最后一句话。

我对此也感到困惑。事实证明,创建的小部件必须使用
show()
show\u all()
显式显示。就你而言:

    row0.show_all()

作为最后一句话。

记住,您必须在运行时显示所有内容,无论是在每个小部件上显示()还是在主窗口等顶层显示所有内容。请记住,您必须在运行时显示所有内容,无论是在每个小部件上显示()还是在主窗口等顶层显示所有内容。谢谢。谢谢对不起,我为这个超级简单的东西发疯了。谢谢。谢谢对不起,我为这个超级简单的东西发疯了。