在python中,使用Gtk 3.0,我尝试使用笔记本获取Windows。在此笔记本中,有一些扩展器。每个扩展器将包含一个树视图。
经过一番搜索,我有这段代码:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
# Create liststore which will be contained within treeview
# liststore has 3 rows and 3 columns
self.store = Gtk.ListStore(str, str, float)
self.store.append(["string1.1", "string1.2", 1.0])
self.store.append(["string2.1", "string2.2", 2.0])
self.store.append(["string3.1", "string3.2", 3.0])
# Create treeview which will be contained within one expander
# treeview has 3 columns and contain liststore
self.tree = Gtk.TreeView(self.store)
for i, column_title in enumerate(["col1", "col2", "col3"]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
self.tree.append_column(column)
# Addind a callback to selection_changed event of treeview
select = self.tree.get_selection()
select.connect("changed", self.on_tree_selection_changed)
# Create expander which will be contained within one notebook page
# Adding treeview inside expander
self.page1 = Gtk.Expander()
self.page1.set_border_width(10)
self.page1.add(self.tree)
# Create notebook and adding expander inside first page
self.notebook = Gtk.Notebook()
self.add(self.notebook)
self.notebook.append_page(self.page1, Gtk.Label('Page1'))
self.Window = Gtk.Window()
self.Window.add(self.notebook)
self.show_all()
def on_tree_selection_changed(self, selection):
model, treeiter = selection.get_selected()
if treeiter != None:
print("You selected", model[treeiter][0])
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
我无法在扩展器中显示treeview。
但没有扩展器,则显示树视图。
我在Ubuntu 12.04上工作,并使用
python ihm.py
命令启动程序。有人可以向我解释为什么它不起作用?
最佳答案
您要将笔记本添加到父级两次。删除线
self.Window.add(self.notebook)
这对我有用。很奇怪您没有收到关于此的错误消息。
关于python - 带有扩展器内Treeview的GTK 3.0窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43184517/