#! /usr/bin/python # File: switches.py # Makes 7 switches with 7 bulbs and connections between them from Tkinter import * def flipswitch(x): print "switch",x pass root = Tk() ## create a Tk root widget. This is an ordinary window K = 7 ## number of switches N = K ## number of bulbs switchrow = 2 ## where to put the switches in the grid bulbrow = switchrow+1 ## where to put the bulbs ## make labels and put them on these two rows switchlabel = Label(root, text="switches", bg="red") switchlabel.grid(row=switchrow,column=0) bulblabel = Label(root, text="bulbs") bulblabel.grid(row=bulbrow,column=0) blip = IntVar() blip.set(1) blop = IntVar() blop.set(0) def blah(): print "you pressed the button!" print "the value of blip is ", blip.get() print "the value of blop is ", blop.get() blop.set(1) print "the value of blop is ", blop.get() pass extra = Checkbutton( root , text="ex" , command=blah, variable=blip) extra.grid( row=7, column=4) extra2 = Checkbutton( root , text="ex2" , variable=blop) extra2.grid( row=7, column=5) ## make the switches s =[] ## this list will contain the Variables associated with the switches switches = [] ## this list will contain the graphical switch objects for k in range(K) : s.append( IntVar() ) ## this makes the integer variable s[k] functionname = eval('lambda: flipswitch('+str(k)+')' ) ## create the string lambda: flipswitch(1) ## and evaluate it, thus creating a function. ## This makes switches[k] switches.append( Checkbutton( root , text=k , command=functionname , variable=s[k] ) ) switches[k].grid(row=switchrow,column=k+1) root.mainloop()