Mica devo dire cosa sono i pulsanti vero? Dico invece che tenterò di non chiamarli bottoni. E che ce ne sono millemila, alcuni inutili.
L’oggetto Button
class Gtk.Button([label[, stock[, use_underline]]])
se
stock
non è None
mette l’immagine e il testo da stock item.
set_label(label)
setta il testo dell’etichetta.
set_use_underline(use_underline)
se True
il carattere successivo a _
diventa un acceleratore.
#!/usr/bin/python3 #b0.py from gi.repository import Gtk class ButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Button Demo") self.set_border_width(10) hbox = Gtk.Box(spacing=6) self.add(hbox) button = Gtk.Button("Click Me") button.connect("clicked", self.on_click_me_clicked) hbox.pack_start(button, True, True, 0) button = Gtk.Button(stock=Gtk.STOCK_OPEN) button.connect("clicked", self.on_open_clicked) hbox.pack_start(button, True, True, 0) button = Gtk.Button("_Close", use_underline=True) button.connect("clicked", self.on_close_clicked) hbox.pack_start(button, True, True, 0) def on_click_me_clicked(self, button): print("\"Click me\" button was clicked") def on_open_clicked(self, button): print("\"Open\" button was clicked") def on_close_clicked(self, button): print("Closing application") Gtk.main_quit() win = ButtonWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()
ToggleButton
se clickato resta premuto fino al prossimo click, come l’interruttore della luce.
class Gtk.ToggleButton([label[, stock[, use_underline]]])
tutto come Gtk.Button.
get_active()
ritorna True
se il pulsante è premuto, altrimenti False
.
set_active(is_active)
per avere il pulsante premuto passare True
, per sollevarlo False
. Quest’azione equivale al click sul pulsante.
#!/usr/bin/python3 #tb.py from gi.repository import Gtk class ToggleButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="ToggleButton Demo") self.set_border_width(10) hbox = Gtk.Box(spacing=6) self.add(hbox) button = Gtk.ToggleButton("Button 1") button.connect("toggled", self.on_button_toggled, "1") hbox.pack_start(button, True, True, 0) button = Gtk.ToggleButton("B_utton 2", use_underline=True) button.set_active(True) button.connect("toggled", self.on_button_toggled, "2") hbox.pack_start(button, True, True, 0) def on_button_toggled(self, button, name): if button.get_active(): state = "on" else: state = "off" print("Button", name, "was turned", state) win = ToggleButtonWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()
CheckButton
È simile a ToggleButton, cambia solo la rappresentazione grafica.
class Gtk.CheckButton([label[, stock[, use_underline]]])
tutto come Gtk.Button.
#!/usr/bin/python3 #cb.py from gi.repository import Gtk class CheckButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="CheckButton Demo") self.set_border_width(10) hbox = Gtk.Box(spacing=6) self.add(hbox) button = Gtk.CheckButton("Button 1") button.connect("toggled", self.on_button_checked, "1") hbox.pack_start(button, True, True, 0) button = Gtk.CheckButton("B_utton 2", use_underline=True) button.set_active(True) button.connect("toggled", self.on_button_checked, "2") hbox.pack_start(button, True, True, 0) def on_button_checked(self, button, name): if button.get_active(): state = "on" else: state = "off" print("Button", name, "was turned", state) win = CheckButtonWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()
Notare (righe 17 e 22) l’uso di toggled
e non .checked
RadioButton
Simili ai CheckButton con la differenza che lavorano in gruppo: ce n’è sempre uno e uno solo selezionato.
Possono essere creati con uno di questi metodi: Gtk.RadioButton.new_from_widget()
, Gtk.RadioButton.new_with_label_from_widget()
o Gtk.RadioButton.new_with_mnemonic_from_widget()
.
Nel creare il primo pulsante gli si passa None
come gruppo. Il primo pulsante creato è selezionato, cosa che è possibile cambiare con Gtk.ToggleButton.set_active()
.
class Gtk.RadioButton
static new_from_widget(group)
aggiunge il pulsante a group
; se group è None
un nuovo gruppo viene creato.
static new_with_label_from_widget(group, label)
come il precedente con la definizione dell’etichetta.
static new_with_mnemonic_from_widget(group, label)
come il precedente con _
che identifica l’acceleratore (scorciatoia da tastiera).
join_group(group)
aggiunge questo pulsante al gruppo group
.
#!/usr/bin/python3 #rb.py from gi.repository import Gtk class RadioButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="RadioButton Demo") self.set_border_width(10) hbox = Gtk.Box(spacing=6) self.add(hbox) button1 = Gtk.RadioButton.new_with_label_from_widget(None, "Button 1") button1.connect("toggled", self.on_button_toggled, "1") hbox.pack_start(button1, False, False, 0) button2 = Gtk.RadioButton.new_from_widget(button1) button2.set_label("Button 2") button2.connect("toggled", self.on_button_toggled, "2") hbox.pack_start(button2, False, False, 0) button3 = Gtk.RadioButton.new_with_mnemonic_from_widget(button1, "B_utton 3") button3.connect("toggled", self.on_button_toggled, "3") hbox.pack_start(button3, False, False, 0) def on_button_toggled(self, button, name): if button.get_active(): state = "on" else: state = "off" print("Button", name, "was turned", state) win = RadioButtonWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()
Nota: mi accorgo adesso che gli acceleratori funzionano (Alt-u per Button 3 ma non vengono visualizzati; panico! o non usarli).
Prossimamente altri pulsanti 😉