why does a disabled button respond to clicks

Ronnie van 't Westeinde ronnews at westeindeweb.nl
Mon Dec 24 05:28:24 EST 2007


Hi,

Your code doesn't work because you're directly binding to a mouse
click event. The proper (high-level) way of configuring which command
is called when the button is pressed is simply to configure the button with
the command parameter:
button.configure(command=some_function)

Your application modified looks like this, and works as expected. Note
that a function configured for command doesn't have any parameters.

Kind regards,
Ronnie

----
from Tkinter import *
class MyApp:
    def __init__(self,parent):
        self.mainframe=Frame(parent)
        self.mainframe.pack()

        #self.canvorig=Canvas(self.mainframe,width=100,height=200)
        #self.canvorig.pack()
        self.button1=Button(self.mainframe,bg="green")
        self.button1.configure(text="1",command=self.buttonClick1)
        self.button1.pack(side=LEFT)
        self.button2=Button(self.mainframe,bg="yellow")
        self.button2.configure(text="2",command=self.buttonClick2)
        self.button2.pack(side=LEFT)
        self.button3=Button(self.mainframe,bg="red")
        self.button3.configure(text="3",command=self.buttonClick3)
        self.button3.pack(side=RIGHT)

    def buttonClick1(self):
        print "ok clicked"
        self.button2.configure(state="disabled")
        self.button2.update_idletasks()

    def buttonClick2(self):
        print "2 clicked"

    def buttonClick3(self):
        print "3 clicked"
        self.button2.configure(state="normal")
        self.button2.update_idletasks()

root = Tk()
myapp = MyApp(root)
root.mainloop()

<damonjulian at yahoo.com> wrote in message 
news:9168ecd1-1100-47db-83b7-6e3f04a44cd3 at d21g2000prf.googlegroups.com...
> hi ,i created 3 buttons such that if button1 is clicked it will
> disable button2 ,and clicking button3 will restore state of button2 to
> normal,
>
> to my dismay i find that button2 still responds to clicks even if it
> is greyed out
>
> here is the code..am i doing something wrong? is there a way to truly
> disable the button?
>
>
>
> class MyApp:
> def __init__(self,parent):
> self.mainframe=Frame(parent)
> self.mainframe.pack()
>
> #self.canvorig=Canvas(self.mainframe,width=100,height=200)
> #self.canvorig.pack()
> self.button1=Button(self.mainframe,bg="green")
> self.button1.configure(text="1")
> self.button1.pack(side=LEFT)
> self.button1.bind("<Button-1>",self.buttonClick1)
> self.button2=Button(self.mainframe,bg="yellow")
> self.button2.configure(text="2")
> self.button2.pack(side=LEFT)
> self.button2.bind("<Button-1>",self.buttonClick2)
> self.button3=Button(self.mainframe,bg="red")
> self.button3.configure(text="3")
> self.button3.pack(side=RIGHT)
> self.button3.bind("<Button-1>",self.buttonClick3)
>
> def buttonClick1(self,event):
> print "ok clicked"
> self.button2.configure(state="disabled")
> self.button2.update_idletasks()
>
> def buttonClick2(self,event):
> print "2 clicked"
>
> def buttonClick3(self,event):
> print "3 clicked"
> self.button2.configure(state="normal")
> self.button2.update_idletasks()
>
> root = Tk()
> myapp = MyApp(root)
> root.mainloop()
> 





More information about the Python-list mailing list