gpt4 book ai didi

python - 使用 python 和 GObject 在 glade 中添加组合框

转载 作者:行者123 更新时间:2023-11-28 22:03:47 25 4
gpt4 key购买 nike

我正在尝试将组合框应用于小型 GTK3 界面,但我真的不知道如何填充它的列表,以及如何将界面的组合框与我的 python 代码连接起来。

有人可以通过一个小例子告诉我怎么做吗?剩下的我可以完成。

最佳答案

ComboBoxes,像 TextViews 或 TreeViews 一样,是明确区分 View (它看起来像什么)和模型(它们包含什么信息)的小部件。你需要在 Glade 中做:

  1. 将组合框添加到 GUI 中的某个位置。
  2. 创建一个将保存数据的 ListStore。将 Liststore 配置为您需要的任何列(每列都有一个类型)。
  3. 返回组合框,将之前创建的Liststore 设置为模型。
  4. 编辑组合框(右键单击,编辑)并添加单元格渲染器。映射该单元格渲染器以显示来自模型某些列的数据。
  5. 如果您的数据是静态的,您可以在 Glade 中向您的 ListStore 添加行。如果您的数据是动态的,您将需要在代码中获取列表存储,然后用与您的列表存储具有相同类型元素的列表填充它。

我能想到的较小的例子是这个:

测试.glade

<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkListStore" id="myliststore">
<columns>
<!-- column-name code -->
<column type="gchararray"/>
<!-- column-name legible -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="window_position">center-always</property>
<property name="default_width">400</property>
<signal name="destroy" handler="main_quit" swapped="no"/>
<child>
<object class="GtkBox" id="box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Best color in the world:</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="mycombobox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">myliststore</property>
<signal name="changed" handler="combobox_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText" id="renderer"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

测试.py

from gi.repository import Gtk
from os.path import abspath, dirname, join

WHERE_AM_I = abspath(dirname(__file__))

class MyApp(object):

def __init__(self):
# Build GUI
self.builder = Gtk.Builder()
self.glade_file = join(WHERE_AM_I, 'test.glade')
self.builder.add_from_file(self.glade_file)

# Get objects
go = self.builder.get_object
self.window = go('window')
self.myliststore = go('myliststore')
self.mycombobox = go('mycombobox')

# Initialize interface
colors = [
['#8C1700', 'Redish'],
['#008C24', 'Greenish'],
['#6B6BEE', 'Blueish'],

]
for c in colors:
self.myliststore.append(c)
self.mycombobox.set_active(0)

# Connect signals
self.builder.connect_signals(self)

# Everything is ready
self.window.show()

def main_quit(self, widget):
Gtk.main_quit()

def combobox_changed(self, widget, data=None):
model = widget.get_model()
active = widget.get_active()
if active >= 0:
code = model[active][0]
print('The code of the selected color is {}'.format(code))
else:
print('No color selected')

if __name__ == '__main__':
try:
gui = MyApp()
Gtk.main()
except KeyboardInterrupt:
pass

亲切的问候

关于python - 使用 python 和 GObject 在 glade 中添加组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8376139/

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