GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Nov 21, 2014 8:38 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: PYGTK : Dynamically adding Items to CellRendererCombo
PostPosted: Fri Aug 24, 2012 12:01 pm 
Offline

Joined: Fri Aug 24, 2012 11:49 am
Posts: 3
I am trying add items to CellRendererCombo dynamically. See the code below. The second column is combobox. When I click on 'Samsung' the corresponding items is listed in the combo box. Now the problem is, when I click 'LG', the items corresponds to 'Samsung' is listed. In short, the combobox is showing Items for the previously selected key, not for the current key.

How to solve this Issue??? Thanks in advance. :)
Code:
    #!/usr/bin/env python

    import gtk,os

    class CellRendererCombo:
        def __init__(self):
            window = gtk.Window()
            window.set_default_size(200, 200)

            liststore_hardware = gtk.ListStore(str, str)
            liststore_hardware.append(["Television", "Samsung"])
            liststore_hardware.append(["Mobile Phone", "LG"])
            liststore_hardware.append(["DVD Player", "Sony"])

            treeview = gtk.TreeView(liststore_hardware)
            column_text = gtk.TreeViewColumn("Text")
            column_combo = gtk.TreeViewColumn("Combo")
            treeview.append_column(column_text)
            treeview.append_column(column_combo)

            cellrenderer_text = gtk.CellRendererText()
            column_text.pack_start(cellrenderer_text, False)
            column_text.add_attribute(cellrenderer_text, "text", 0)

            self.cellrenderer_combo = gtk.CellRendererCombo()
            self.cellrenderer_combo.set_property("editable", True)

            self.cellrenderer_combo.set_property("text-column", 0)
            column_combo.pack_start(self.cellrenderer_combo, False)
            column_combo.add_attribute(self.cellrenderer_combo, "text", 1)

            window.connect("destroy", lambda w: gtk.main_quit())
            self.cellrenderer_combo.connect("editing-started", self.populate_combo)
            self.cellrenderer_combo.connect("edited", self.combo_changed, liststore_hardware)

            window.add(treeview)
            window.show_all()

        def combo_changed(self, widget, path, text, model):
            model[path][1] = text

        def populate_combo(self,*args):
            liststore_manufacturers = gtk.ListStore(str)
            manufacturers={}
            manufacturers['Samsung'] = ["Samsung", "Item1","Item2", "Item3", "Item4"]
            manufacturers['LG'] = ["LG", "Item5", "Item6", "Item7", "Item8", "Item9",'Item10']
            manufacturers['Sony'] = ["Sony", "Item11", "Item12", "Item13", "Item14", "Item15",'Item16']

            catgry=self.cellrenderer_combo.get_property ("text") # read the content of combobox

            for item in manufacturers[catgry]:
                liststore_manufacturers.append([item])
            self.cellrenderer_combo.set_property("model", liststore_manufacturers)

    #       
    CellRendererCombo()
    gtk.main()



Top
 Profile  
 
 Post subject: Re: PYGTK : Dynamically adding Items to CellRendererCombo
PostPosted: Fri Aug 24, 2012 12:37 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello and welcome to the GTK+ forums!

You're having troubles because model on combo box that pops-up is set before GtkCellEditable::editing-started signal is emitted. So when you set model in editing started handler, you set it for the next time combo box is popped-up.

Why don't you simply store your models on a per-row basis and simply let the property binding machinery do all the work for you?

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: PYGTK : Dynamically adding Items to CellRendererCombo
PostPosted: Mon Aug 27, 2012 11:07 am 
Offline

Joined: Fri Aug 24, 2012 11:49 am
Posts: 3
Hello tadeboro,

Thank you for the replay.

Can you be more specific about the point "store your models on a per-row basis and simply let the property binding machinery do all the work for you".


Top
 Profile  
 
 Post subject: Re: PYGTK : Dynamically adding Items to CellRendererCombo
PostPosted: Tue Aug 28, 2012 2:19 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

I meant something like this:

Code:
#!/usr/bin/env python

import gtk,os

class CellRendererCombo:
    def __init__(self):
        window = gtk.Window()
        window.set_default_size(200, 200)

        liststore_hardware = gtk.ListStore(str, str, gtk.TreeModel)

        items = (("Television", ("Samsung", "Item11", "Item12")),
                 ("Mobile Phone", ("LG", "Item21", "Item22")),
                 ("DVD Player", ("Sony", "Item31", "Item32")))

        for item in items:
            store = gtk.ListStore(str)
            for i in item[1]:
                iter = store.append([i])
            liststore_hardware.append([item[0], item[1][0], store])

        treeview = gtk.TreeView(liststore_hardware)
        column_text = gtk.TreeViewColumn("Text")
        column_combo = gtk.TreeViewColumn("Combo")
        treeview.append_column(column_text)
        treeview.append_column(column_combo)

        cellrenderer_text = gtk.CellRendererText()
        column_text.pack_start(cellrenderer_text, False)
        column_text.add_attribute(cellrenderer_text, "text", 0)

        self.cellrenderer_combo = gtk.CellRendererCombo()
        self.cellrenderer_combo.set_property("editable", True)
        self.cellrenderer_combo.set_property("text-column", 0)
        column_combo.pack_start(self.cellrenderer_combo, False)
        column_combo.add_attribute(self.cellrenderer_combo, "text", 1)
        column_combo.add_attribute(self.cellrenderer_combo, "model", 2)

        window.connect("destroy", lambda w: gtk.main_quit())
        self.cellrenderer_combo.connect("edited", self.combo_changed, liststore_hardware)

        window.add(treeview)
        window.show_all()

    def combo_changed(self, widget, path, text, model):
        model[path][1] = text

#       
CellRendererCombo()
gtk.main()


Tadej


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group