Tkinter Questions

johngrayson at home.com johngrayson at home.com
Mon Oct 1 07:23:05 EDT 2001


Building on Laura's example, you can substitute images of
whatever makes sense...

import Tkinter

class LongButton(Tkinter.Label):
    def __init__(self, parent, **kw):
        self.neutralI=Tkinter.PhotoImage(file='neutral.gif')
        self.leftI=Tkinter.PhotoImage(file='left.gif')
        self.rightI=Tkinter.PhotoImage(file='right.gif')
        kw['image']=self.neutralI
        if kw.has_key('lcommand'):
            self.left=kw['lcommand']
            del kw['lcommand']
        else:
            self.left=self.printit
            
        if kw.has_key('rcommand'):
            self.right=kw['rcommand']
            del kw['rcommand']
        else:
            self.right=self.printit
            
        Tkinter.Label.__init__(self, parent, **kw)
        self.bind("<ButtonRelease-1>", self.got_pressed)

    def printit(self, arg):
        print 'you clicked %s' % arg

    def got_pressed(self, event):
        if event.x <= self.winfo_width()/2:
            self.left('on the left side')
	    self.configure(image=self.leftI)
        else:
            self.right('on the right side')
	    self.configure(image=self.rightI)
            
def sayleft(self):
        print 'LEFT LEFT LEFT'

def sayright(self):
        print 'RIGHT RIGHT RIGHT'

root = Tkinter.Tk()
quit = Tkinter.Button(root, text="QUIT", fg="red", command=root.quit)
quit.pack(side='left')
b = LongButton(root)
b2 =  LongButton(root, lcommand=sayleft, rcommand=sayright)
b.pack(side='left')
b2.pack(side='left')

root.mainloop()






More information about the Python-list mailing list