TreeView不在Python Gtk3中工作

TreeView不在Python Gtk3中工作,python,python-3.x,gtk3,gtktreeview,Python,Python 3.x,Gtk3,Gtktreeview,因此,我有一个GTk结构,如: Window()> Grid()> Label() + ScrolledWindow()> TreeView() + Box()> Button() 除如下所示,滚动窗口未正确显示外,其他一切正常。我相信我的定位有问题 我试着摆弄定位号码,但没能成功 我的代码是: import gi gi.r

因此,我有一个GTk结构,如:

Window()>
    Grid()>
        Label() 
        +
        ScrolledWindow()>
            TreeView()
        +    
        Box()>
            Button()
除如下所示,滚动窗口未正确显示外,其他一切正常。我相信我的定位有问题

我试着摆弄定位号码,但没能成功

我的代码是:

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()
导入gi
gi.require_版本('Gtk','3.0')
从gi.repository导入Gtk
index=None#保存最终选定字幕索引的全局变量
类窗口(Gtk.window):
定义初始化(自身,数据):
Gtk.Window.\uuuu初始(self,title=“Subsekerv7研发”)
自行设置边框宽度(5)
设置默认大小(200400)
self.grid=Gtk.grid()
self.grid.set\u column\u齐次(True)
self.grid.set_rowndex=无
标题_text=Gtk.Label()
标题\u文本。设置\u标记('选择下面的副标题\n\n选择一个副标题,然后按下载\n')
scrolled_window=Gtk.scrolled window()
滚动窗口。设置边框宽度(5)
滚动的_窗口。设置_策略(Gtk.PolicyType.AUTOMATIC,Gtk.PolicyType.AUTOMATIC)
self.data_list_store=Gtk.ListStore(str,str,str,str)
对于数据中的项:self.data\u list\u store.append(列表(项[:4]))
self.data_tree_view=Gtk.TreeView(self.data_list_store)
对于i,枚举中的列标题([“序列”、“名称”、“语言”、“分数”、]):
renderer=Gtk.CellRendererText()
column=Gtk.TreeViewColumn(列标题,呈现器,文本=i)
self.data\u tree\u view.append\u column(column)
滚动的_窗口。使用_视口添加_(self.data_树视图);
按钮盒=Gtk.盒(方向=Gtk.方向.水平,间距=10)
#按钮声明
self.submit_button=Gtk.button(label=“下载”)
self.submit\u按钮。连接(“单击”,self.select\u句柄)
self.cancel_按钮=Gtk.按钮(label=“cancel”)
self.cancel_按钮.connect(“单击”,lambda x:self.destroy())
#向按钮框添加按钮
按钮\u框。打包\u开始(self.submit\u按钮,True,True,0)
按钮\u框。打包\u开始(self.cancel\u按钮,真,真,0)
self.grid.attach(标题\文本,0,0,4,1)
self.grid.attach(滚动窗口,0,1,4,4)
自网格连接(按钮盒,0,5,4,1)
self.add(self.grid)
def select_句柄(自身,小部件):
全球指数
tree\u sel=self.data\u tree\u view.get\u selection()
(tm,ti)=树选择获取选定对象()
index=tm.get_值(ti,0)#将索引值修改为树视图中当前选定的索引
自我毁灭
def main():
w=窗口([('a'*30,'b','c','d','e'),('p'*30,'q','r','s','t'))#伪测试数据
w、 连接(“销毁”,Gtk.main_退出)
w、 全部展示
Gtk.main()
如果uuuu name_uuuuuu=='uuuuuu main:main()

原因是GTK的小部件布局行为。小部件占用的空间不会超过默认情况下所需的空间。
滚动窗口
将变为不可见,因为它会收缩为零(内容的大小无关紧要)

这可以通过使用
set\u size\u request(width,height)
强制特定大小来解决,或者使用
set\u属性('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)
网格
的替代方法是使用
,并在函数中设置
expand=True