gpt4 book ai didi

python - 具有两列的组合框

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:09 25 4
gpt4 key购买 nike

我正在尝试创建一个包含两列的GtkComboBox。我举了一些例子,并试图适应但没有成功。仅显示第二列的项目。

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ComboBox(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_title("ComboBox")
self.set_default_size(150, -1)
self.connect("destroy", Gtk.main_quit)

slist = Gtk.ListStore(str, str)
slist.append(['01', 'Ferro'])
slist.append(['07', 'Uranio'])
slist.append(['08', 'Cobalto'])

combobox = Gtk.ComboBox()
combobox.set_model(slist)
combobox.set_active(0)
combobox.set_wrap_width(2)
self.add(combobox)

cellrenderertext = Gtk.CellRendererText()
combobox.pack_start(cellrenderertext, True)
combobox.add_attribute(cellrenderertext, "text", 1)

window = ComboBox()
window.show_all()

Gtk.main()

我想创建一个像这样的GtkComboBox:

enter image description here

最佳答案

要显示的每一列都必须有一个 CellRendererText。

cell1 = Gtk.CellRendererText()
cell2 = Gtk.CellRendererText()
combobox.pack_start(cell1, True)
combobox.pack_start(cell2, True)
combobox.add_attribute(cell1, "text", 0)
combobox.add_attribute(cell2, "text", 1)

关于python - 具有两列的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39184776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com