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

sil*_*ash 4 pygobject python-3.x gtk3

我在Gtk3中使用python3,我基本上需要remove从我的一些小部件中删除并Gtk.Window在a Gtk.Button是时将其替换为其他小部件clicked。我已经尝试将Gtk.ListBoxes与Gtk.ListBoxRows 一起使用,到目前为止我可以从中删除所有Rows,ListBox但是当我尝试添加它们时返回,基本上什么也没发生。这是我的代码的一部分:

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)
Run Code Online (Sandbox Code Playgroud)

该功能将得到一个Gtk.ListBox尝试,当作为argument.And什么我到目前为止remove()add()事情列表框是:

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]))
Run Code Online (Sandbox Code Playgroud)

顺便说一下,如果有一种简单的方法可以随时随地替换PyGObject中的小部件,请让我知道我在相关问题上看到的大部分答案,而这些答案完全没有用

ykt*_*too 5

我对此也感到困惑。事实证明,创建的窗口小部件必须使用show()或显式显示show_all()。在您的情况下:

    row0.show_all()
Run Code Online (Sandbox Code Playgroud)

作为最后的陈述。