You are not logged in.

#1 2009-11-28 23:40:10

robrene
Member
Registered: 2009-04-16
Posts: 168

[PyGTK + GtkBuilder] Dynamically add items to ComboBox

I'm writing a little python+gtk application that allows for some easy customisation of the icon theme. I used Glade3 to create a GtkBuilder project, and I load it using gtk.Builder. I'm running into some trouble trying to populate my comboboxes though...

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#      3.141592653589793238462643383279
#    5028841971693993751058209749445923
#   07816406286208998628034825342117067
#   9821    48086         5132
#  823      06647        09384
# 46        09550        58223
# 17        25359        4081
#           2848         1117
#           4502         8410
#           2701         9385
#          21105        55964
#          46229        48954
#          9303         81964
#          4288         10975
#         66593         34461            -con
#        284756         48233
#        78678          31652        71
#       2019091         456485       66
#      9234603           48610454326648
#     2133936            0726024914127
#     3724587             00660631558
#     817488               152092096


import sys
import os
import gtk


class Picon:

    def on_MainWindow_destroy(self, widget, data=None):

        gtk.main_quit()


    def on_CloseButton_clicked(self, button, data=None):

        gtk.main_quit()


    def on_AboutButton_clicked(self, button, data=None):

        if self.about_dialog:
            self.about_dialog.present()
            return

        authors = [ "Rob (robrene) Spoel <robspoel@googlemail.com>" ]

        about_dialog = gtk.AboutDialog()
        about_dialog.set_transient_for(self.window)
        about_dialog.set_destroy_with_parent(True)
        about_dialog.set_name("π-con")
        about_dialog.set_version("0.1")
        about_dialog.set_copyright("Copyright © 2009 Rob Spoel")
        about_dialog.set_comments("A tool to tweak the FIXME icon theme.")
        about_dialog.set_authors(authors)
        about_dialog.set_logo_icon_name("picon")

        def close(dialog, response, editor):
            picon.about_dialog = None
            dialog.destroy()

        def delete_event(dialog, event, editor):
            picon.about_dialog = None
            return True

        about_dialog.connect("response", close, self)
        about_dialog.connect("delete-event", delete_event, self)

        self.about_dialog = about_dialog
        about_dialog.show()


    def init_folders(self):

        cbox = self.builder.get_object("FolderCBox")
        cbox.append_text("Current (no change)")


    def init_brands(self):

        cbox = self.builder.get_object("BrandCBox")
        cbox.append_text("Current (no change)")


    def __init__(self):

        self.about_dialog = None

        try:
            self.builder = gtk.Builder()
            self.builder.add_from_file("picon.glade")
        except:
            self.error_message = "Couldn't load 'picon.glade'"
            sys.exit(1)

        self.window = self.builder.get_object("MainWindow")

        self.builder.connect_signals(self)

        self.init_folders()
        self.init_brands()


    def main(self):

        self.window.show()
        gtk.main()


if __name__ == "__main__":
    picon = Picon()
    picon.main()

I once wrote a PyGTK application using libglade a long time ago, and this was how it was done (using gtk.glade.XML.get_widget() instead of gtk.Builder.get_object()). However, now I get the following warnings:

[rob@rob-thinkpad Purity Ultimate]$ ./picon.py 
./picon.py:81: GtkWarning: gtk_combo_box_append_text: assertion `GTK_IS_LIST_STORE (combo_box->priv->model)' failed
  cbox.append_text("Current (no change)")
./picon.py:87: GtkWarning: gtk_combo_box_append_text: assertion `GTK_IS_LIST_STORE (combo_box->priv->model)' failed
  cbox.append_text("Current (no change)")

I'm not sure what it means, nor how to solve it... What is the easiest way to just populate the comboboxes with some text of my choosing?


smile neutral sad big_smile yikes wink hmm tongue lol mad roll cool

Offline

#2 2009-11-29 03:17:31

linkmaster03
Member
Registered: 2008-12-27
Posts: 269

Re: [PyGTK + GtkBuilder] Dynamically add items to ComboBox

self.combobox = builder.get_object('combobox')
cell = gtk.CellRendererText()
self.combobox.pack_start(cell)
self.combobox.add_attribute(cell, 'text', 0)



self.combobox.append_text("Option 1")
self.combobox.append_text("Option 2")

Unfortunately, you cannot add CellRenderers through Glade, so that's why you have to do it manually in the code.

Last edited by linkmaster03 (2009-11-29 03:17:56)

Offline

#3 2009-11-29 12:00:49

robrene
Member
Registered: 2009-04-16
Posts: 168

Re: [PyGTK + GtkBuilder] Dynamically add items to ComboBox

I'M afraid that didn't fix it, I still get the same warning, and the combobox is still empty. I did some more reading, and I believe I may have to add some sort of 'model', whatever that may be. I see Glade3 also has an option called 'ComboBox model' in the widget properties.

What I also found out is that '(...) the "model" property which (...) is acting as data source for the combo_box (...)' : http://www.pygtk.org/docs/pygtk/class-g … -get-model

There is also a method called set_model, but I don't really know what to feed it and how I can modify that so that it holds the options I want...


smile neutral sad big_smile yikes wink hmm tongue lol mad roll cool

Offline

#4 2009-11-29 12:56:16

robrene
Member
Registered: 2009-04-16
Posts: 168

Re: [PyGTK + GtkBuilder] Dynamically add items to ComboBox

Alright, after some more trial and error I finally managed to get it working, I'm posting the relevant code here if anyone ever stumbles accross the same problem:

cbox = builder.get_object("my_combobox")

store = gtk.ListStore(gobject.TYPE_STRING)
store.append(["foobar"])
cbox.set_model(store)

cell = gtk.CellRendererText()
cbox.pack_start(cell, True)
cbox.add_attribute(cell, 'text', 0)

cbox.set_active(0)

Last edited by robrene (2009-11-29 12:58:30)


smile neutral sad big_smile yikes wink hmm tongue lol mad roll cool

Offline

#5 2009-11-30 20:34:53

linkmaster03
Member
Registered: 2008-12-27
Posts: 269

Re: [PyGTK + GtkBuilder] Dynamically add items to ComboBox

Yes, I forgot to mention that you need a ListStore. You can create this, and link it to the combo box, through Glade. You can also set the ListStore type through Glade:

30novmon1535.png

Sorry I forgot to point that out; I was only looking at my code.

Offline

Board footer

Powered by FluxBB