简体   繁体   English

使用Pixbuf的PyGtk CellRendererCombo

[英]PyGtk CellRendererCombo using Pixbuf

I would like to display a ComboBox in my TreeView that contains some Icons. 我想在包含一些图标的TreeView中显示一个ComboBox。 So I created a ListStore to hold the data. 因此,我创建了一个ListStore来保存数据。

    # Initialize a list store for the combobox.
    priorityModel = Gtk.ListStore(GObject.TYPE_INT, GdkPixbuf.Pixbuf)
    priorityModel.append([0, self.loadPixbuf('./data/media/flag_blue.png')])
    priorityModel.append([1, self.loadPixbuf('./data/media/flag_green.png')])
    priorityModel.append([2, self.loadPixbuf('./data/media/flag_yellow.png')])
    priorityModel.append([3, self.loadPixbuf('./data/media/flag_red.png')])

Then I created the Gtk.CellRendererCombo object and assigned the above Gtk.ListStore as model. 然后,我创建了Gtk.CellRendererCombo对象,并将上述Gtk.ListStore分配为模型。

    # Setup the priority cell renderer, ...
    self.priorityRenderer = Gtk.CellRendererCombo()
    self.priorityRenderer.set_property( 'editable', True )
    self.priorityRenderer.set_property("model", priorityModel)
    self.priorityRenderer.connect("edited", self.on_priority_changed, self.listStore, 3)

Finally I created a new Gtk.TreeViewColumn and assigned the CellRenderer and added it to the TreeView. 最后,我创建了一个新的Gtk.TreeViewColumn并分配了CellRenderer并将其添加到TreeView中。

    # ... setup the priority column ...
    self.colPriority = Gtk.TreeViewColumn("Priority", self.priorityRenderer, text=3 )

    # ... and add it to the treeview.
    self.append_column( self.colPriority )

All nice but how could I display the Pixbuf icon instead of the integer and how do I initialize the ComboBox using the integer? 一切都很好,但是如何显示Pixbuf图标而不是整数?如何使用整数初始化ComboBox? And, second question, is it possible to show only the icon when the combobox is not active and show the icon and some text when the value is changed (ComboBox active)? 还有,第二个问题,是否可以在组合框未激活时仅显示图标,并在更改值时显示图标和一些文本(组合框处于活动状态)?

You need a Gtk.CellRendererPixbuf to display the icon. 您需要一个Gtk.CellRendererPixbuf来显示图标。 See for example the excellent Python 3 Gtk+ Tutorial . 例如,请参见出色的Python 3 Gtk +教程

Answering your second question: Yes. 回答第二个问题:是的。 Just connect to the "changed" signal of the Gtk.CellRendererCombo and add/remove the cell renderer dynamically. 只需连接到Gtk.CellRendererCombo"changed"信号,然后动态添加/删除单元格渲染器即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM