Using checkbox with python and Glade -
i'm new on using glade, yet i've made small examples , have never tried using checkbuttons, , make gui in following image:
the idea on , off buttons, print different message depending on checkbuttons selected. i've read what's on site " " , few more, dont have minimal idea how start. have @ least 1 such example: if checkbutton selected print: checkbutton selected , check whether selected or not.
to run interface know, did this:
from gi.repository import gtk builder = gtk.builder() builder.add_from_file("port_manager.glade") handlers = { } builder.connect_signals(handlers) window = builder.get_object("windowport") window.show_all() gtk.main()
can please give me simple example of using checkbox please ? did gui in glade manually not programming.
first of all, you're using gtk.builder connect_signals
method assumes you've declared signal handler method names (callback methods) via glade.
anyway, can programmatically. example, have common callback knows checkbutton triggered , useful (more messy unless code reusable) or setup individual handlers/callbacks each checkbutton.
let's take example , setup handlers. approach be, first 3 checkboxes, attach concrete callback checkbox 1 , 2 , attach generic handler checkboxes 1, 2 , 3:
import gi gi.require_version('gtk', '3.0') gi.repository import gtk def on_checkb1_toggled(button): if button.get_active(): state = "active" else: state = "inactive" print "checkbutton 1 toggled, state " + state def on_checkb2_toggled(button): if button.get_active(): state = "active" else: state = "inactive" print "checkbutton 2 toggled, state " + state def on_checkbutton_toggled(button, name): if button.get_active(): state = "active" else: state = "inactive" print "common handler: checkbutton " + name + " toggled, state " + state builder = gtk.builder() builder.add_from_file("port_manager.glade") handlers = { } builder.connect_signals(handlers) window = builder.get_object("windowport") ## added code checkb1 = builder.get_object("checkbutton1") checkb2 = builder.get_object("checkbutton2") checkb3 = builder.get_object("checkbutton3") # ... checkb1.connect ("toggled", on_checkb1_toggled) checkb2.connect ("toggled", on_checkb2_toggled) checkb1.connect ("toggled", on_checkbutton_toggled, "1") checkb2.connect ("toggled", on_checkbutton_toggled, "2") checkb3.connect ("toggled", on_checkbutton_toggled, "3") window.connect("destroy", gtk.main_quit) ## end added code window.show_all() gtk.main()
running code, console output (example):
$ python checkbuttons.py checkbutton 1 toggled, state active common handler: checkbutton 1 toggled, state active checkbutton 2 toggled, state active common handler: checkbutton 2 toggled, state active common handler: checkbutton 3 toggled, state active common handler: checkbutton 3 toggled, state inactive common handler: checkbutton 3 toggled, state active
as can see, common method (on_checkbox_toggled
) triggered checkboxes 1, 2 , 3 , can identify them name. checkboxes 1 , 2 have concrete , distinct handler (on_checkb1_toggled
, on_checkb2_toggled
respectively).
you can choose approach suits best. advise check python gtk 3 tutorial has examples can try.
good luck.
Comments
Post a Comment