【发布时间】:2017-10-31 08:18:46
【问题描述】:
我将 python3 与 Gtk3 一起使用,我需要基本上 remove 我的 Gtk.Window 中的一些小部件,并在 Gtk.Button 为 clicked 时将它们替换为其他小部件。我尝试将 Gtk.ListBoxes 与 @ 一起使用987654326@s 到目前为止我可以从ListBox 中删除所有Rows 但是当我尝试将它们添加回来时,基本上什么都没有发生。这是我的代码的一部分:
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() 放入列表框时,我所拥有的是:
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_all()。
标签: python-3.x gtk3 pygobject