Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

enter image description here

Hi , I have some question to ask

I just want to disable the button when i start my program

in attached image, it looks like the button is already disabled ,but its response to my click event or keyboard event

What should i do ?

Thank you for all answer

from Tkinter import *


def printSomething(event):
    print("Print")

#Start GUI
gui = Tk()
gui.geometry("800x500")
gui.title("Button Test")

mButton = Button(text="[a] Print",fg="#000",state="disabled")

mButton.place(x=5,y=10)

mButton.bind('<Button-1>',printSomething)
gui.bind('a',printSomething)

gui.mainloop()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
382 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to unbind the event. state="disabled"/state=DISABLED makes button disabled but it doesn't unbind the event. You need to unbind the corresponding events to achieve this goal. If you want to enable the button again then you need to bind the event again. Like:

from Tkinter import *

def printSomething(event):
    print("Print")

#Start GUI
gui = Tk()
gui.geometry("800x500")
gui.title("Button Test")

mButton = Button(text="[a] Print",fg="#000",state="disabled")

mButton.place(x=5,y=10)

mButton.bind('<Button-1>',printSomething)
mButton.unbind("<Button-1>") #new line added
gui.bind('a',printSomething)

gui.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...