如何在tkinter条目小部件中输入某些文本时启用按钮?

时间:2016-10-05 07:08:58

标签: python tkinter

目前我有一个禁用按钮的场景。现在我想在用户在tkinter条目小部件中输入一些输入时启用按钮。

请建议。

谢谢!

2 个答案:

答案 0 :(得分:2)

当用户按bind()时,您Entry可以执行<Key>功能。

请参阅Events and Binding

中的bind()<Key>

答案 1 :(得分:0)

你可以按照这个question和@Furas说你可以将函数绑定到Entry,就像这样

from Tkinter import Tk, Entry   
root = Tk()

frame = Frame(root) #"frame" represents the parent window, where the entry widget should be placed.

frame.pack()
#GUI widgets
entry = Entry(frame, width=80) #The syntax of an entry widget 

entry.pack(side='right')

#callbacks
def enableEntry():
    entry.configure(state="normal")
    entry.update()

def disableEntry():
    entry.configure(state="disabled")
    entry.update()



def click(key):
    #print the key that was pressed
     print key.char    


var = StringVar()
disableEnButton = Radiobutton(frame, text="Disable", variable=var, value="0", command=disableEntry)
disableEnButton.pack(anchor=W)
enableEnButton = Radiobutton(frame, text="Enable", variable=var, value="1", command=enableEntry)
enableEnButton.pack(anchor=W)


#Bind entry to any keypress
entry.bind("<Key>", click)   
root.mainloop() #This is necessary for the event loop to service events such as button clicks