why does a disabled button respond to clicks

D

damonjulian

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()
 
R

Ronnie van 't Westeinde

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top