On 4/27/05, Daniele Medri <[EMAIL PROTECTED]> wrote:
> i coded something like that:
> 
>         self.cbox = gtk.combo_box_new_text()
>         self.cbox.append_text("1 day")
>         self.cbox.append_text("1 week")
>         self.cbox.append_text("4 weeks")
>         self.cbox.set_active(0)
> 
> and grabbed "something" with
> 
>         def get_active_text(cbox):
>                 model = cbox.get_model()
>                 active = cbox.get_active()
>                 if active < 0:
>                         return None
>                 return model[active][0]
> 
> as describe in pygtk tutorial.
> Well, I don't need the text label.. I need a value associated to that
> label. Question 1, how to add a label+value keypair, Question2 how to
> retrieve this active value.

Lots of ways.

You could keep track of values/strings/etc. in a dictionary. You could
also use the model in the combo box to keep track of stuff (put the
label and the value both in the model, set the CB to use the label
column, and grab the value column of the model in your callback). In
my mind, that's a little to complicated -- the following (untested)
code is simple and extensible/generalizable to me anyway:

default_choice = '1 day'
choices = [('1 day','value1'),
                 ('1 week','value2'),
                 ('4 weeks', 'value3'),
                ]
cb_values = {}
self.cbox = gtk.combo_box_new_text()

# initialize box
for n,choice_and_val in enumerate(choices):
         choice,val = choice_and_val
         self.cbox.append_text(choice)
         cb_values[n]=val

self.cbox.set_active(cb_values[default_choice])

def  get_active_text (cbox):
    return cb_values[cbox.get_active()]

Hope this helps!
Tom
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to