Domanda

I'm trying to implement a drop down list in an Ubuntu app using Glade (GTK+3) and Python. I can get the ComboBoxText to display, populated with strings. However when I close the window it is contained in and then re-open it, the combobox is not there, just a completely blank window.

I added the ComboBoxText widget in question to a window in Glade. I then added this code in my Python programme:

def on_button_edit_clicked(self, widget):
    """ display list of events already stored, and allow deletion """   

    self.combo = self.builder.get_object('combo_box')
    self.store = Gtk.ListStore(str)

    self.store.append(['hello'])
    self.store.append(['goodbye'])
    self.combo.set_model(self.store)

    event_editor = self.builder.get_object("event_editor")
    event_editor.show() 

I even tried destroying the widget to see if that helps:

def on_event_editor_destroy(self, widget):
    self.combo.destroy()
    self.store = Gtk.ListStore(str)
    self.combo.set_model(self.store)

EDIT:

I have since tried some alternative code, that included a CellRendererText object, but still no luck.

These are my sources: I created a Gtk.ListStore(), then I created a Gtk.CellRendererText(), then I created a Gtk.ComboBox(). This was all in-line with example 13.3 here.

Nothing works. When I open the combobox window a 2nd time, it is just a blank window. Can anyone help please?

È stato utile?

Soluzione

I've found a solution. I was closing the window using the x button, which must have been destroying the widget and it's associated list store. Instead I am now using a custom button that merely hides the window.

Altri suggerimenti

The title says, "ComboBoxText in Glade / GTK+3 / Python..." then u try to implement a GtkComboBox, not a GtkComboBoxText. Why torture yourself with a GtkComboBox.

Glade file (snippet)

<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>

Notice no GtkListStore is needed for a GtkComboBoxText. Now some class methods

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top