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

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


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

我尝试过尝试定位数字,但无法正常进行。

python - TreeView在Python Gtk3中不起作用-LMLPHP

我的代码是:

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的小部件布局行为。窗口小部件占用的空间不会超过默认情况下所需的空间。 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

关于python - TreeView在Python Gtk3中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51810333/

10-12 18:25