Search code examples
pythoncomboboxgtkgtk3

how to shows options when create class and inheritance from Gtk.ComboBox


I made class named Combo and inheritance from Gtk.ComboBox in GtkClass.py:

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

class Combo(Gtk.ComboBox):

    def __init__(self,opt):
        Gtk.ComboBox.__init__(self)
        self.options = opt
        self.set()

    def set(self):
        self.id_kitchen_list = Gtk.ListStore(str)
        for item in self.options:
            self.id_kitchen_list.append([item])

        self.id_kit_combobox = Gtk.ComboBox.new_with_model(self.id_kitchen_list)
        renderer_text = Gtk.CellRendererText()
        self.id_kit_combobox.pack_start(renderer_text, True)
        self.id_kit_combobox.add_attribute(renderer_text, "text", 0)
        return self.id_kit_combobox

when I use this class in my main.py file:

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

from GtkClass import Combo
class Assistant(Gtk.Assistant):
    def __init__(self):
        Gtk.Assistant.__init__(self)
        self.set_title("Assistant")
        self.set_default_size(600, -1)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.append_page(box)
        self.set_page_type(box, Gtk.AssistantPageType.INTRO)
        self.set_page_title(box, "Page 1: Kitchen")

        grid = Gtk.Grid()
        grid.set_row_spacing(10)
        grid.set_column_spacing(10)

        self.id_kit_combobox = Combo(["1", "2", "3", "4", "5"])
        grid.attach(self.id_kit_combobox, 1, 0, 1, 1)

        box.pack_start(grid, True, True, 0)
        self.set_page_complete(box, True)

assistant = Assistant()
assistant.show_all()

Gtk.main()

ComboBox creates but shows no options when I click on it!

enter image description here enter image description here


Solution

  • Question: class inheritance from Gtk.ComboBox

    First, i want to show what is wrong with your implementation.

    class Combo(Gtk.ComboBox):
    
        def __init__(self,opt):
    
    1. Here you __init__ the inherieted Gtk.ComboBox, but you didn't pass your model=. Therefore you get a ComboBox with a default model.
          Gtk.ComboBox.__init__(self)
          ...
      
    2. Here, you instantiate the ListStore you want to use, but you don't .set... this ListStore to the allready instantiated self object which is of type Gtk.ComboBox.
      Therefore the appended options, never shows up.
      def set(self):
          self.id_kitchen_list = Gtk.ListStore(str)
          for item in self.options:
              self.id_kitchen_list.append([item])
      
    3. Here, you instantiate a new object of type Gtk.ComboBox which leads to ComboBox in ComboBox. This inner ComboBox will never show up.
          self.id_kit_combobox = Gtk.ComboBox.new_with_model(self.id_kitchen_list)
          ...
      
    4. Here, you return a object which is never used from the calling self.set().
          return self.id_kit_combobox
      

    Solution:

    class Combo(Gtk.ComboBox):
        def __init__(self, opt):
            self.id_kitchen_list = Gtk.ListStore(str)
            Gtk.ComboBox.__init__(self, model=self.id_kitchen_list)
    
            for item in opt:
                self.id_kitchen_list.append([item])
    
            renderer_text = Gtk.CellRendererText()
            self.pack_start(renderer_text, True)
            self.add_attribute(renderer_text, "text", 0)