2011-08-12

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:

  1. activate the explosive
  2. 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

widget.set_sensitive(False)

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

#!/usr/bin/env 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(selfwidget):
    #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__":
  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.

Comments
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
2 plus 4
Answer:
required
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008