无法将滚动条添加到 Python GTK3 笔记本中的 TreeView

Unable to add a Scrollbar to TreeView present in a notebook in Python GTK3

我刚开始使用 Python Gtk。 我正在尝试设计一个包含 3 个选项卡的视图(这是使用 Gtk.Notebook() 完成的)。 在其中一个选项卡中,我添加了一个 ComboBoxText 和一个 TreeView。 我正在尝试向树视图添加滚动条,但无法这样做。这是我的代码供参考

import gi 
# Since a system can have multiple versions 
# of GTK + installed, we want to make 
# sure that we are importing GTK + 3. 
gi.require_version("Gtk", "3.0") 
from gi.repository import Gtk, Pango

# list of available drivers
drivers_available = [
    "All",
    "drv_ccfl",
    "drv_uart",
    "drv_adc",
    "drv_gpio",
    "drv_ffs",
    "drv_can",
    "drv_pwm",
    "drv_temp",
    "drv_volt",
    "drv_photo",
    "drv_ble",
    "drv_gpu"
    ]
    
# list of tuples for each software, containing the software name, initial release, and main programming languages used
driver_parameters_list = [
    ("drv_ccfl", "param1","string", "xx" ),
    ("drv_uart", "param2", "int", "10"),
    ("drv_adc", "param3", "float", "9.5"),
    ("drv_gpio", "param5", "bool", "true"),
    ("drv_ffs", "param6", "int", "0"),
    ("drv_can", "param7", "float", "54.0"),
    ("drv_pwm", "param8", "int",  "20"),
    ("drv_temp", "param9", "bool", "false"),
    ("drv_volt", "param10", "string", "xyz"),
    ("drv_photo", "param11", "string", "abc"),
    ("drv_ble", "param12", "float", "35.9"),
    ("drv_gpu", "param13", "int", "24"),
    ("drv_ccfl", "param14","int", "45" ),
    ("drv_uart", "param15", "int", "25"),
    ("drv_adc", "param16", "string", "yy"),
    ("drv_gpio", "param17", "float", "13.9"),
    ("drv_ffs", "param18", "int", "0"),
    ("drv_can", "param19", "string", "xyz"),
    ("drv_pwm", "param20", "float",  "3.6"),
    ("drv_temp", "param21", "int", "27"),
    ("drv_ble", "param12", "float", "35.9"),
    ("drv_gpu", "param13", "int", "24"),
    ("drv_ccfl", "param14","int", "45" ),
    ("drv_uart", "param15", "int", "25"),
    ("drv_adc", "param16", "string", "yy"),
    ("drv_gpio", "param17", "float", "13.9"),
    ("drv_ffs", "param18", "int", "0"),
    ("drv_can", "param19", "string", "xyz"),
    ("drv_pwm", "param20", "float",  "3.6"),
    ("drv_temp", "param21", "int", "00")
    ]

columns = ["Driver_type", "Parameter", "type", "Value"]

class MyWindow(Gtk.Window): 
    def __init__(self): 
        Gtk.Window.__init__(self, title ="Pricol") 
        self.set_border_width(10) 
        #self.set_default_size(1000, 200) 
        #self.set_resizable(False)
        
        # Create a Box and add it to the Window widget
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20) 
        vbox.set_property("height-request", 500)
        self.add(vbox)
        
        self.notebook = Gtk.Notebook() 
        self.notebook.set_scrollable(True)
        self.notebook.set_show_border(True)
        self.notebook.set_tab_pos(0)
        vbox.pack_start(self.notebook, False, False, 0)
        
        # Add a box to Page 1 in order to be able to add more widgets
        self.page1 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20) 
        
        self.page1.set_border_width(50) 

        # Add a comboBox for the dropdown
        drivers_comboBox = Gtk.ComboBoxText() 
        drivers_comboBox.connect("changed", self.on_drivers_comboBox_changed)
        drivers_comboBox.set_entry_text_column(0) 
        drivers_comboBox.set_wrap_width(1)
        self.page1.add(drivers_comboBox)
        
        # Add drivers in the list
        for driver in drivers_available: 
            drivers_comboBox.append_text(driver) 
            
        drivers_comboBox.set_active(0) 
        
        ##########
        # Create a tree for the driver paramenter list
        # The data in the model (three strings for each row, one for each column        
        listmodel = Gtk.ListStore(str, str, str, str)
        # append the values in the model
        for i in range(len(driver_parameters_list)):
            listmodel.append(driver_parameters_list[i])
        
        self.driver_filter = listmodel.filter_new()
        self.driver_filter.set_visible_func(self.driver_filter_func)
        
        # a treeview to see the data stored in the model
        view = Gtk.TreeView(model=self.driver_filter)
        # for each column
        for i, column in enumerate(columns):
            # cellrenderer to render the text
            cell = Gtk.CellRendererText()
            # the text in the first column should be in boldface
            if i == 0:
                cell.props.weight_set = True
                #cell.props.weight = Pango.Weight.BOLD
            # the column is created
            col = Gtk.TreeViewColumn(column, cell, text=i)
            col.set_min_width(250)
            # and it is appended to the treeview
            view.append_column(col)
        
        view.set_grid_lines(3)
        
        grid = Gtk.Grid()
        grid.attach(view, 0, 0, 1, 1)
    
        self.page1.add(grid) 
        ##########
        
        
        # Add Page 1 as a Tab
        self.page1_title = Gtk.Label()
        self.page1_title.set_property("height-request", 50)
        self.page1_title.set_markup("<b><big>Drivers</big></b>")
        self.page1_title.set_property("width-request", 150)
        self.notebook.append_page(self.page1, self.page1_title ) 
        
                
        # Add a box to Page 2 in order to be able to add more widgets
        self.page2 = Gtk.Box() 
        self.page2.set_border_width(50) 
        
        # Add widgets to page 2 here
        
        ##
        
        # Add Page 2 as a tab
        self.page2_title = Gtk.Label()
        self.page2_title.set_property("height-request", 50)
        self.page2_title.set_markup("<b><big>Data</big></b>")
        self.page2_title.set_property("width-request", 150)
        self.notebook.append_page(self.page2, self.page2_title )
        
        
        
        
        # Add a box to Page 3 in order to be able to add more widgets
        self.page3 = Gtk.Box() 
        self.page3.set_border_width(50) 
        
        # Add widgets to page 3 here
        
        ##
        
        # Add Page 3 as a tab
        self.page3_title = Gtk.Label()
        self.page3_title.set_property("height-request", 50)
        self.page3_title.set_markup("<b><big>View</big></b>")
        self.page3_title.set_property("width-request", 150)
        self.notebook.append_page(self.page3, self.page3_title )
        
        
    def on_drivers_comboBox_changed(self, combo):
        # we set the current language filter to the button's label
        self.current_filter_driver = combo.get_active_text()
        # we update the filter, which updates in turn the view
        self.driver_filter.refilter()
    
    
    def driver_filter_func(self, model, iter, data):
        """Tests if the language in the row is the one in the filter"""
        if (
            self.current_filter_driver is None
            or self.current_filter_driver == "All"
        ):
            return True
        else:
            return model[iter][0] == self.current_filter_driver
            
    
    def on_selection_button_clicked(self, widget):
        """Called on any of the button clicks"""
        # we set the current language filter to the button's label
        self.current_filter_driver = widget.get_label()
        # we update the filter, which updates in turn the view
        self.driver_filter.refilter()
        
        
        
win = MyWindow() 
win.connect("destroy", Gtk.main_quit) 
# Display the window. 
win.show_all() 
# Start the GTK + processing loop 
Gtk.main() 

视图的大小随着列表大小的增加而不断增加。 此外,当我在下拉列表中使用过滤器时,视图会再次调整大小。 有人可以帮我解决这个问题吗?

只需在视图和网格之间添加 ScrolledWindow

...
        view.set_grid_lines(3)

        scrolled_window = Gtk.ScrolledWindow(vexpand=True, hexpand=True)
        scrolled_window.add(view)
        
        grid = Gtk.Grid()
        grid.attach(scrolled_window, 0, 0, 1, 1)
    
        self.page1.add(grid) 
...