TreeView在Python Gtk3中不起作用

乌布都·萨玛德(Ubdus Samad)

所以我有一个GTk结构,例如:

Window()>
    Grid()>
        Label() 
        +
        ScrolledWindow()>
            TreeView()
        +    
        Box()>
            Button()

一切正常,除非Scrolled Window无法正确显示,如下所示。我相信我在定位上做错了什么。

我尝试过尝试定位数字,但无法使其正常工作。

Gtk窗口的屏幕截图

我的代码是:

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的小部件布局行为。窗口小部件占用的空间不会超过默认情况下所需的空间。当AScrolledWindow缩小为零时,它将变为不可见(内容的大小无关紧要)。

这可以通过使用强制使用特定大小来解决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)

a的替代方法Grid是使用Box,并expand=Truepack_start函数中进行设置

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章