2
在python中,與Gtk 3.0,我嘗試獲得與筆記本的窗口。在這個筆記本里面,有一些擴展器。每個擴展器都將包含一個樹視圖。Gtk 3.0窗口與樹型內部擴展器
一些搜索之後,我有這樣的代碼:
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()
我不管理,以顯示內部擴展樹視圖。 但沒有擴展器,顯示樹形視圖。
我在ubuntun 12.04上工作,用python ihm.py
命令啓動我的程序。
有些人可以解釋爲什麼它不起作用?
非常感謝。有效地,它在執行時抑制警告消息。但是不允許我的程序運行(擴展器總是空的)。 –
有一些奇怪的事情發生。我只是再次檢查,它適用於我。 – theGtknerd
我犯了一個錯誤。我在Ubuntu 12.04上工作。但是告訴我,如果我錯了,因爲我們使用相同版本的gtk,這並不重要? –