标题说,“Glade / GTK+3 / Python 中的ComboBoxText ...”然后你尝试实现一个GtkComboBox,而不是一个GtkComboBoxText。为什么要用 GtkComboBox 折磨自己。
林间空地文件(片段)
<object class="GtkComboBoxText" id="comboboxtextEventEditor">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">10</property>
<property name="hexpand">True</property>
<property name="entry_text_column">0</property>
<property name="id_column">1</property>
<signal name="changed" handler="on_changed_event_editor" swapped="no"/>
</object>
注意 GtkComboBoxText 不需要 GtkListStore。现在一些类方法
from __future__ import print_function
from gi.repository import Gtk
import os
def TheClass(Gtk.ApplicationWindow):
#class variable
UI_FILE = "preference.glade"
@staticmethod
def get_id():
return "windowTheClass"
def __init__(self, app):
""" Constructor"""
# Initialize class variables.
#app is an instance of a class which extends Gtk.Application
#Hardcode to have a working example code
self.ui_path = os.path.join("home", "useraccount", "Documents", "example")
#self.app = app
#self.ui_path = self.app.ui_path
# Load ui
self.__load_ui()
self.__initialize_event_editor_combobox()
def __load_ui(self):
self.builder = Gtk.Builder()
try:
strUIFile = os.path.join(self.ui_path, self.UI_FILE)
self.builder.add_from_file( strUIFile )
del strUIFile
self.win = self.builder.get_object( self.__class__.get_id() )
# This is not useful until Gtk version 3.6
#self.app.add_window(self.win)
self.builder.connect_signals(self)
except:
print(self.__class__.__name__ + ".__load_ui error", sys.exc_info()[1])
def __initialize_event_editor_combobox(self):
""" Populate event editor comboboxtext"""
combobox = self.builder.get_object("comboboxtextEventEditor")
combobox.remove_all()
# Args [position, id, text]
combobox.insert(0,"0", "Hello")
combobox.insert(0,"1", "goodbye")
combobox.set_active_id("1")
del combobox
def on_changed_event(self, widget, data=None):
""" combobox value selected. Refresh combobox"""
strSelectedEntry = widget.get_active_text()
print(self.__class__.__name__ + ".on_changed_event", strSelectedEntry)
#self.initialize_something_else(strSelectedEntry)
del strSelectedEntry