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

SP07

我刚刚开始使用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() 

视图的大小随着列表大小的增加而不断增加。另外,当我在下拉菜单中使用过滤器时,视图会重新调整大小。有人可以帮我吗?

elya5

只需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) 
...

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将滚动条添加到警报中

将滚动条添加到闪亮的框中

将滚动条添加到具有innerhtml属性的div中

将滚动条添加到没有设置高度的div中

如何将滚动条添加到react-bootstrap Modal.Body中

将滚动条添加到CSS网格布局中的每一列

将滚动条添加到包含Thumbail图像的Groupbox中

将滚动条添加到pyqt4中的网格布局

python tkinter按钮在画布网格中绑定并将滚动条添加到画布

在 Bootstrap 4 中添加新项目后溢出时将滚动条添加到表格

如何将滚动条添加到包含一堆网格按钮的框架中?

如何编写用于将滚动条添加到SharePoint列表列中的单元格的JSON代码?

在Tkinter中将滚动条添加到一组小部件中

问题:当我使用bootstrap 4网格系统将图像添加到列中时,出现水平滚动条

TreeView在Python Gtk3中不起作用

将颜色条添加到python中的和弦图上

无法在 Tkinter 中向主窗口添加滚动条

在 tkinter python 中添加滚动条以更新标签?

Javafx中的滚动条无法滚动

需要在带有滚动条的 ttk 笔记本中安装鼠标滚轮

如何将拥抱脸库添加到Kaggle笔记本中

将生命周期配置添加到 SageMaker 中的现有笔记本?

检查id是否存在于数组中并将项添加到数组中

如何在bluemix spark上将罐子添加到python笔记本中?

无法在Jupyter笔记本中绘图

无法在Tkinter TreeView中启用滚动条

在jupyter笔记本python中密谋

在wordpress中添加滚动条

Visual Studio扩展性:Rectange -s添加到ScrollbarMargin的Children类中,以防止鼠标单击滚动条