I combobox consentono di scegliere da un dropdown menu. L’alternativa sarebbe di avere un gruppo di radio buttons che però occupano più spazio. Se le scelte sono parecchie possono anche essere disposti a griglia (ma controllate il progetto, sicuri che debba essere così?).
Inoltre è possibile consentire all’utente di inserire testo libero.
Oggetti ComboBox
class Gtk.ComboBox
static new_with_entry()
crea un Gtk.ComboBox
vuoto.
static new_with_model(model)
crea un Gtk.ComboBox
vuoto con il modello inizializzato a model
.
static new_with_model_and_entry(model)
crea un nuovo Gtk.ComboBox
con un entry e il modello inizializzato a model
.
get_active_iter()
ritorna un Gtk.TreeIter
che punta all’elemento corrente o None
se nessuno è attivo.
set_model(model)
setta il modello model
; se None
il precedente, se esiste, viene rimosso.
get_model()
ritorna il Gtk.TreeModel
attivo per questo combo.
set_entry_text_column(text_column)
setta il modello della colonna; text_column
dev’essere di tipo str
. Solo se il modello ha la proprietà has-entry
True
. (Non è che ci ho capito tanto).
set_wrap_width(width)
roba per la griglia; posso ignorarlo.
Oggetti ComboBoxText
class Gtk.ComboBoxText
static new_with_entry()
crea un nuovo Gtk.ComboBoxText
.
append_text(text)
aggiunge text
alla lista di stringhe immagazzinate nel combo.
get_active_text()
ritorna la stringa corrente o None
se non c’è selezione.
#!/usr/bin/python3 # combo.py from gi.repository import Gtk class ComboBoxWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="ComboBox Example") self.set_border_width(10) name_store = Gtk.ListStore(int, str) name_store.append([1, "Billy Bob"]) name_store.append([11, "Billy Bob Junior"]) name_store.append([12, "Sue Bob"]) name_store.append([2, "Joey Jojo"]) name_store.append([3, "Rob McRoberts"]) name_store.append([31, "Xavier McRoberts"]) vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) name_combo = Gtk.ComboBox.new_with_model_and_entry(name_store) name_combo.connect("changed", self.on_name_combo_changed) name_combo.set_entry_text_column(1) vbox.pack_start(name_combo, False, False, 0) country_store = Gtk.ListStore(str) countries = ["Austria", "Brazil", "Belgium", "France", "Germany", "Switzerland", "United Kingdom", "United States of America", "Uruguay"] for country in countries: country_store.append([country]) country_combo = Gtk.ComboBox.new_with_model(country_store) country_combo.connect("changed", self.on_country_combo_changed) renderer_text = Gtk.CellRendererText() country_combo.pack_start(renderer_text, True) country_combo.add_attribute(renderer_text, "text", 0) vbox.pack_start(country_combo, False, False, True) currencies = ["Euro", "US Dollars", "British Pound", "Japanese Yen", "Russian Ruble", "Mexican peso", "Swiss franc"] currency_combo = Gtk.ComboBoxText() currency_combo.set_entry_text_column(0) currency_combo.connect("changed", self.on_currency_combo_changed) for currency in currencies: currency_combo.append_text(currency) vbox.pack_start(currency_combo, False, False, 0) self.add(vbox) def on_name_combo_changed(self, combo): tree_iter = combo.get_active_iter() if tree_iter != None: model = combo.get_model() row_id, name = model[tree_iter][:2] sel = "Selected: ID = " + str(row_id) + " name = " + name print(sel) else: entry = combo.get_child() sel = "Entered: " + entry.get_text() print(sel) def on_country_combo_changed(self, combo): tree_iter = combo.get_active_iter() if tree_iter != None: model = combo.get_model() country = model[tree_iter][0] print("Selected: country = ", country) def on_currency_combo_changed(self, combo): text = combo.get_active_text() if text != None: print("Selected: currency = ", text) win = ComboBoxWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()
e anche