When initialising the images for use on the button, remember to do ".show()" on BOTH of the icons, otherwise, only the one visible on the button on startup will be visible later on.Disclaimer: I'm still getting my head around GTK (PyGTK), so the following code snippet may not be considered "best practice" in these cricles.
# "Lock" toolbar toggle
class LockToolbarToggle(gtk.ToggleToolButton):
# Class Constants ========================
# (gtk.Image) icon widgets for the states; Initialised on first use
LOCKED = None
UNLOCKED = None
# Setup ===================================
# ctor
# < label_template: (str) label for the item, with format placeholders for
# lock/unlock substitution
# < tooltip_template: (str) tooltip for the item, with format palceholders
# for lock/unlock substitution
def __init__(self, label_template, tooltip_template):
# init settings
gtk.ToggleToolButton.__init__(self)
self.label_template = label_template
self.tooltip_template = tooltip_template
# set up icons to use
# NOTE: as last step of init, we must "show" the icon, otherwise it
# won't show up later when toggled
if LockToolbarToggle.LOCKED is None:
LockToolbarToggle.LOCKED = gtk.Image()
LockToolbarToggle.LOCKED.set_from_file("icons/locked.svg")
LockToolbarToggle.LOCKED.show()
if LockToolbarToggle.UNLOCKED is None:
LockToolbarToggle.UNLOCKED = gtk.Image()
LockToolbarToggle.UNLOCKED.set_from_file("icons/unlocked.svg")
LockToolbarToggle.UNLOCKED.show()
# register callback for updating displayed state on state-change
# and call this once with dummy args to set the right values
self.connect("toggled", self.state_changed_cb, None)
self.state_changed_cb(self)
# Signals =================================
# callback for updating visual state on state change
def state_changed_cb(self, widget, data=None):
if self.get_active():
self.set_label(self.label_template % ("Unlock"))
self.set_tooltip(self.tooltip_template % ("Disable"))
self.set_icon_widget(LockToolbarToggle.LOCKED)
else:
self.set_label(self.label_template % (" Lock"))
self.set_tooltip(self.tooltip_template % ("Enable"))
self.set_icon_widget(LockToolbarToggle.UNLOCKED)
No comments:
Post a Comment