In the movies, when it is time to detonate the explosives, the person setting off the KABOOM has a sweet remote control with either a two part switch or with two separate switches. Damn, I love a good movie explosion.
The two part switch allows the user to:
- activate the explosive
- blow shit up
Because the switch has two components, activate and detonate, it is uncommon for there to be accidental explosions. This is a damn fine safety measure and when adding the ability to block an identi.ca user with heybuddy, I wanted a similar safety precaution; blocking someone by accident should not be possible. So that is what I did.
To accomplish this, it was necessary to do the following
- create a disabled button that brings the hammer down!
- create a checkbox that will activate the button
In GTK parlance, to disable a button, one needs to set the button sensitivity to false. Since I am using Python, this is accomplished thusly
To show this idea as a small working bit of code, I wrote a quick Python-GTK app that almost does the bare minimum.
Enter the Python
import gtk
class application:
def __init__(self):
#I want to keep track of click counts
self.click_count=0
'''build the UI'''
#make the window
winder = gtk.Window(gtk.WINDOW_TOPLEVEL)
winder.connect("destroy",self.quit)
#winder.set_default_size(300,300)
winder.set_title("Super Trigger")
#make some boxes
vbox = gtk.VBox()
hbox = gtk.HBox()
#make some buttons
self.button = gtk.Button("Click Me")
self.checkbutton = gtk.CheckButton(label="Enable Button")
#add the buttons to the hbox
hbox.pack_start(self.button,False,False)
hbox.pack_start(self.checkbutton,False,False)
#add the hbox to the vbox
vbox.pack_start(hbox,False,False)
#create a Label
self.label = gtk.Label()
#add the label to the vbox
vbox.pack_start(self.label)
#add the vbox to the window
winder.add(vbox)
#disable the button
self.button.set_sensitive(False)
#connect the buttons
self.checkbutton.connect('toggled', self.toggle_clicked )
self.button.connect('clicked', self.button_clicked)
#show it all!
winder.show_all()
def toggle_clicked(self, widget):
#is the checkbutton active?
active = widget.get_active()
self.button.set_sensitive(active)
def button_clicked(self,widget):
#increment the click count
self.click_count+=1
#make a string
string = "clicks: %d" %(self.click_count)
#set the label to the string
self.label.set_text(string)
def run(self):
gtk.main()
def quit(self,widget=None):
gtk.main_quit()
if __name__=="__main__":
a = application()
a.run()
It may not be the best solution, but it is a solution and it works.
Well that's it for now. Quit reading, and go write some code.