TreeView 在 Python Gtk3 中不工作
TreeView not working in Python Gtk3
所以我的 GTk 结构如下:
Window()>
Grid()>
Label()
+
ScrolledWindow()>
TreeView()
+
Box()>
Button()
一切正常,除了 Scrolled Window
没有正确显示,如下所示。我相信我在定位或其他方面做错了。
我试过调整定位数字,但无法正常工作。
我的密码是:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
index = None #Global variable holding the index of the final chosen subtitle
class window(Gtk.Window):
def __init__(self,data):
Gtk.Window.__init__(self, title="SubseekerV7 R&D")
self.set_border_width(5)
self.set_default_size(200, 400)
self.grid = Gtk.Grid()
self.grid.set_column_homogeneous(True)
self.grid.set_rowndex = None
heading_text = Gtk.Label()
heading_text.set_markup('<big><b>Choose Subtitle below</b></big>\n\n<i>Select a subtitle and press Download</i>\n')
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_border_width(5)
scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.data_list_store = Gtk.ListStore(str,str,str, str)
for item in data:self.data_list_store.append(list(item[:4]))
self.data_tree_view = Gtk.TreeView(self.data_list_store)
for i, col_title in enumerate(["Serial","Name", "Language", "Score",]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(col_title, renderer, text=i)
self.data_tree_view.append_column(column)
scrolled_window.add_with_viewport(self.data_tree_view);
buttons_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=10)
#Button Declarations
self.submit_button = Gtk.Button(label="Download")
self.submit_button.connect("clicked", self.select_handle)
self.cancel_button = Gtk.Button(label="Cancel")
self.cancel_button.connect("clicked", lambda x:self.destroy())
#Adding buttons to button box
buttons_box.pack_start(self.submit_button, True , True , 0)
buttons_box.pack_start(self.cancel_button, True , True , 0)
self.grid.attach(heading_text, 0, 0, 4, 1)
self.grid.attach(scrolled_window,0,1,4,4)
self.grid.attach(buttons_box,0,5,4,1)
self.add(self.grid)
def select_handle(self,widget):
global index
tree_sel = self.data_tree_view.get_selection()
(tm, ti) = tree_sel.get_selected()
index = tm.get_value(ti, 0) #Modifying the index value to the currently selected index in treeview
self.destroy()
def main():
w = window([('a'*30,'b','c','d','e'),('p'*30,'q','r','s','t')]) #Bogus test dxata
w.connect("destroy", Gtk.main_quit)
w.show_all()
Gtk.main()
if __name__ == '__main__':main()
原因是 GTK 的小部件布局行为。小部件不会占用比默认要求更多的 space。 ScrolledWindow
将变得不可见,因为它缩小到什么都没有(内容的大小无关紧要)。
这可以通过使用 set_size_request(width, height)
强制特定大小来解决,或者使用 set_property('expand', True)
将小部件配置为增长。
示例:
# Setting a fixed height
scrolled_window.set_size_request(-1, 200)
# Configure the scrolled window to expand
scrolled_window.set_property('expand', True)
Grid
的替代方法是使用 Box
,并在 pack_start 函数中设置 expand=True
。
所以我的 GTk 结构如下:
Window()>
Grid()>
Label()
+
ScrolledWindow()>
TreeView()
+
Box()>
Button()
一切正常,除了 Scrolled Window
没有正确显示,如下所示。我相信我在定位或其他方面做错了。
我试过调整定位数字,但无法正常工作。
我的密码是:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
index = None #Global variable holding the index of the final chosen subtitle
class window(Gtk.Window):
def __init__(self,data):
Gtk.Window.__init__(self, title="SubseekerV7 R&D")
self.set_border_width(5)
self.set_default_size(200, 400)
self.grid = Gtk.Grid()
self.grid.set_column_homogeneous(True)
self.grid.set_rowndex = None
heading_text = Gtk.Label()
heading_text.set_markup('<big><b>Choose Subtitle below</b></big>\n\n<i>Select a subtitle and press Download</i>\n')
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_border_width(5)
scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.data_list_store = Gtk.ListStore(str,str,str, str)
for item in data:self.data_list_store.append(list(item[:4]))
self.data_tree_view = Gtk.TreeView(self.data_list_store)
for i, col_title in enumerate(["Serial","Name", "Language", "Score",]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(col_title, renderer, text=i)
self.data_tree_view.append_column(column)
scrolled_window.add_with_viewport(self.data_tree_view);
buttons_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=10)
#Button Declarations
self.submit_button = Gtk.Button(label="Download")
self.submit_button.connect("clicked", self.select_handle)
self.cancel_button = Gtk.Button(label="Cancel")
self.cancel_button.connect("clicked", lambda x:self.destroy())
#Adding buttons to button box
buttons_box.pack_start(self.submit_button, True , True , 0)
buttons_box.pack_start(self.cancel_button, True , True , 0)
self.grid.attach(heading_text, 0, 0, 4, 1)
self.grid.attach(scrolled_window,0,1,4,4)
self.grid.attach(buttons_box,0,5,4,1)
self.add(self.grid)
def select_handle(self,widget):
global index
tree_sel = self.data_tree_view.get_selection()
(tm, ti) = tree_sel.get_selected()
index = tm.get_value(ti, 0) #Modifying the index value to the currently selected index in treeview
self.destroy()
def main():
w = window([('a'*30,'b','c','d','e'),('p'*30,'q','r','s','t')]) #Bogus test dxata
w.connect("destroy", Gtk.main_quit)
w.show_all()
Gtk.main()
if __name__ == '__main__':main()
原因是 GTK 的小部件布局行为。小部件不会占用比默认要求更多的 space。 ScrolledWindow
将变得不可见,因为它缩小到什么都没有(内容的大小无关紧要)。
这可以通过使用 set_size_request(width, height)
强制特定大小来解决,或者使用 set_property('expand', True)
将小部件配置为增长。
示例:
# Setting a fixed height
scrolled_window.set_size_request(-1, 200)
# Configure the scrolled window to expand
scrolled_window.set_property('expand', True)
Grid
的替代方法是使用 Box
,并在 pack_start 函数中设置 expand=True
。