Search code examples
gtkpygobject

GTK ComboBox is empty


I have a Gtk.Grid to which I want to add a row with a combo box, which is initialized with a list. However, my combo box is empty. Can anyone see what I'm missing here?

I used this tutorial as reference

queryTypes = ["Name", "Grade", "Year", "Faculty"]
queryStore = Gtk.ListStore(str)
for qt in queryTypes:
    queryStore.append([qt])

window = builder.get_object("mainWindow")

grid = builder.get_object("queryGrid")
grid.nRows = 1

combox = Gtk.ComboBox.new_with_model(queryStore)
grid.add(combox)

window.show_all()
Gtk.main()

Solution

  • Your combobox is missing a renderer.

    queryTypes = ["Name", "Grade", "Year", "Faculty"]
    queryStore = Gtk.ListStore(str)
    for qt in queryTypes:
        queryStore.append([qt])
    
    window = builder.get_object("mainWindow")
    
    grid = builder.get_object("queryGrid")
    grid.nRows = 1
    
    combox = Gtk.ComboBox.new_with_model(queryStore)
    renderer_text = Gtk.CellRendererText()
    combox.pack_start(renderer_text, True)
    combox.add_attribute(renderer_text, "text", 0)
    grid.add(combox)
    
    window.show_all()
    Gtk.main()