Tkinter Questions

Laura Creighton lac at strakt.com
Sun Sep 30 11:46:51 EDT 2001


Does this do what you want?
-----------
import Tkinter

class LongButton(Tkinter.Button):
    def __init__(self, parent, **kw):
        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.Button.__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')
        else:
            self.right('on the right side')
            
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, text="I behave differently depending on where you click")
b2 =  LongButton(root, text="I behave differently also",
                 lcommand=sayleft, rcommand=sayright)
b.pack(side='left')
b2.pack(side='left')

root.mainloop()
-------------
Laura Creighton




More information about the Python-list mailing list