[pygtk] Looking for help with comboboxes

Gerald Britton gerald.britton at gmail.com
Tue Apr 14 23:06:49 WST 2009


Thanks for the tip.  I hope I can make it work for me.  I'm using
gtkbuilder instead of libglade but the builder combox has no
set_text_column method.  Not sure what to use.  I tried
set_column_span_column but that didn't do it

On Tue, Apr 14, 2009 at 10:48 AM, Lupine <lupine at thelupine.com> wrote:
> Gerald,
>
> This is how I use a combobox.  For this example, read_hostsfile() just
> pulls in an array of hosts:
>
> def populate_cmbox_hostlist(self):
>    """Read in the HOSTSFILE and populate the drop down hosts list"""
>    filelines = read_hostsfile()
>    if filelines:
>        hosts = gtk.ListStore(str)
>        for fileline in filelines:
>            fileline = fileline.strip('\n')
>            hosts.prepend ([fileline])
>        self.widgets.get_widget('host_entry_cmbox').set_model(hosts)
>        self.widgets.get_widget('host_entry_cmbox').set_text_column(0)
>
>
> Doing it this way, also allows me to use entry completion (very cool
> stuff):
>
> def populate_cmbox_hostlist_entry_completion(self):
>    """Read in the HOSTSFILE and populate the entry completion"""
>    filelines = read_hostsfile()
>    if filelines:
>        completion = gtk.EntryCompletion()
>        hosts = gtk.ListStore(str)
>        for fileline in filelines:
>            fileline = fileline.strip('\n')
>            iter = hosts.append()
>            hosts.set(iter, 0, fileline)
>        self.widgets.get_widget('host_entry').set_completion(completion)
>        completion.set_model(hosts)
>        completion.set_text_column(0)
>
>
> Let me know if this points you in the right direction.
> -Lup
>
>
>
> On Tue, 2009-04-14 at 10:39 -0400, Gerald Britton wrote:
>> Hi -- I'm trying to learn how to use comboboxes.  I worked up a little
>> program to try to test it out (below, with imbedded glade).  I tried
>> two methods to get stuff to show in the box, but to no avail.  In the
>> first method I used the append_text method on the combobox.  In the
>> second method I used the append method on the liststore.  Obviously
>> I'm missing something but this newbie can't see what.  I tried both
>> methods separately and together with the same (bad) result.
>>
>> Would someone be able to point out what I am missing?
>>
>> glade = """
>> <?xml version="1.0"?>
>> <interface>
>>   <requires lib="gtk+" version="2.16"/>
>>   <!-- interface-naming-policy project-wide -->
>>   <object class="GtkListStore" id="liststore">
>>     <columns>
>>       <!-- column-name gchararray1 -->
>>       <column type="gchararray"/>
>>     </columns>
>>   </object>
>>   <object class="GtkWindow" id="window">
>>     <property name="title" translatable="yes">Sample window with combo
>> box</property>
>>     <property name="default_width">440</property>
>>     <property name="default_height">250</property>
>>     <signal name="destroy" handler="on_window_destroy"/>
>>     <child>
>>       <object class="GtkComboBox" id="combobox">
>>         <property name="visible">True</property>
>>         <property name="model">liststore</property>
>>       </object>
>>     </child>
>>   </object>
>> </interface>
>> """
>>
>> import gtk
>> g = gtk.Builder()
>> g.add_from_string(glade)
>> g.connect_signals({ "on_window_destroy" : gtk.main_quit })
>>
>> w = g.get_object('window')
>> c = g.get_object('combobox')
>> l = g.get_object('liststore')
>>
>> for i in range(10):
>>   c.append_text("line %d" % i)  # 1. try to append the text to the combobox
>>   l.append(["line %d" % i])        # 2. try to add the text to the
>> liststore in the combobox.
>>
>> w.show()
>> gtk.main()
>>
>>
>
>



-- 
Gerald Britton


More information about the pygtk mailing list