我是tkinter的新手,这是一个简单的方法,但我不知道该怎么做。我想创建一个提交按钮,如果给出相同的用户输入并再次提交按钮将禁用,但如果给出新输入,它将执行其功能。有人可以帮我弄这个吗?谢谢
答案 0 :(得分:0)
如果我理解问题 - 您必须创建列表以记住提交的数据并使用新输入进行检查
import tkinter as tk
# --- functions ---
def submit():
data = e.get()
if data in already_submitted:
print('Sorry already submitted:', data)
else:
print('Submit:', data)
already_submitted.append(data)
# --- main ---
# don't submit empty string
already_submitted = ['']
root = tk.Tk()
e = tk.Entry(root)
e.pack()
b = tk.Button(root, text="Submit", command=submit)
b.pack()
root.mainloop()
顺便说一句:您还可以bind()
运行Entry
,并在每key
次和禁用/启用按钮后检查输入。
import tkinter as tk
# --- functions ---
def submit():
data = e.get()
if data in already_submitted:
print('Sorry already submitted:', data)
else:
print('Submit:', data)
already_submitted.append(data)
b['state'] = 'disable'
def check(event):
#data = event.widget.get()
data = e.get()
if data in already_submitted:
b['state'] = 'disable'
else:
b['state'] = 'normal'
# --- main ---
# don't submit empty string
already_submitted = ['']
root = tk.Tk()
e = tk.Entry(root)
e.pack()
e.bind('<KeyRelease>', check)
b = tk.Button(root, text="Submit", command=submit, state='disable')
b.pack()
root.mainloop()