Ticket #4447: combo.py

File combo.py, 3.3 KB (added by erikos, 10 years ago)

test script

Line 
1from gi.repository import Gtk
2
3class ComboBoxWindow(Gtk.Window):
4
5    def __init__(self):
6        Gtk.Window.__init__(self, title="ComboBox Example")
7
8        self.set_border_width(10)
9
10        name_store = Gtk.ListStore(int, str)
11        name_store.append([1, "Billy Bob"])
12        name_store.append([11, "Billy Bob Junior"])
13        name_store.append([12, "Sue Bob"])
14        name_store.append([2, "Joey Jojo"])
15        name_store.append([3, "Rob McRoberts"])
16        name_store.append([31, "Xavier McRoberts"])
17
18        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
19
20        name_combo = Gtk.ComboBox.new_with_model_and_entry(name_store)
21        name_combo.connect("changed", self.on_name_combo_changed)
22        name_combo.set_entry_text_column(1)
23        vbox.pack_start(name_combo, False, False, 0)
24
25        country_store = Gtk.ListStore(str)
26        countries = ["Austria", "Brazil", "Belgium", "France", "Germany",
27            "Switzerland", "United Kingdom", "United States of America", "Uruguay",
28            "Switzerland", "United Kingdom", "United States of America", "Uruguay",
29            "Switzerland", "United Kingdom", "United States of America", "Uruguay",
30            "Switzerland", "United Kingdom", "United States of America", "Uruguay",
31            "Switzerland", "United Kingdom", "United States of America", "Uruguay",
32            "Switzerland", "United Kingdom", "United States of America", "Uruguay",
33            "Switzerland", "United Kingdom", "United States of America", "Uruguay",
34            "Switzerland", "United Kingdom", "United States of America", "Uruguay"
35                     ]
36        for country in countries:
37            country_store.append([country])
38
39        country_combo = Gtk.ComboBox.new_with_model(country_store)
40        country_combo.connect("changed", self.on_country_combo_changed)
41        renderer_text = Gtk.CellRendererText()
42        country_combo.pack_start(renderer_text, True)
43        country_combo.add_attribute(renderer_text, "text", 0)
44        vbox.pack_start(country_combo, False, False, True)
45
46        currencies = ["Euro", "US Dollars", "British Pound", "Japanese Yen",
47            "Russian Ruble", "Mexican peso", "Swiss franc"]
48        currency_combo = Gtk.ComboBoxText()
49        currency_combo.set_entry_text_column(0)
50        currency_combo.connect("changed", self.on_currency_combo_changed)
51        for currency in currencies:
52            currency_combo.append_text(currency)
53
54        vbox.pack_start(currency_combo, False, False, 0)
55
56        self.add(vbox)
57
58    def on_name_combo_changed(self, combo):
59        tree_iter = combo.get_active_iter()
60        if tree_iter != None:
61            model = combo.get_model()
62            row_id, name = model[tree_iter][:2]
63            print "Selected: ID=%d, name=%s" % (row_id, name)
64        else:
65            entry = combo.get_child()
66            print "Entered: %s" % entry.get_text()
67
68    def on_country_combo_changed(self, combo):
69        tree_iter = combo.get_active_iter()
70        if tree_iter != None:
71            model = combo.get_model()
72            country = model[tree_iter][0]
73            print "Selected: country=%s" % country
74
75    def on_currency_combo_changed(self, combo):
76        text = combo.get_active_text()
77        if text != None:
78            print "Selected: currency=%s" % text
79
80win = ComboBoxWindow()
81win.connect("delete-event", Gtk.main_quit)
82win.show_all()
83Gtk.main()