gpt4 book ai didi

python - 如何验证 tkinter 中的按钮以便该函数只被调用一次?

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:21 25 4
gpt4 key购买 nike

当我多次按下“Enter”按钮时,文本字段将继续显示,但我想要它,这样一旦我输入了我的名字并按下按钮,该按钮就会停用。我该怎么做?

class Enter_Name_Window(tk.Toplevel):
'''A simple instruction window'''
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
self.text = tk.Text(self, width=40, height=2)
self.text.pack(side="top", fill="both", expand=True)
self.text.insert("end", "Please enter your name and class. ")

enter_name = Entry(self)
enter_name.pack()

enter_name.focus_set()

def callback():
self.display_name = tk.Text(self, width=40, height=2)
self.display_name.pack(side="top", fill="both", expand=True)
self.display_name.insert("end", "Now please enter your tutor group.")
tutor = Entry(self)
tutor.pack()

tutor.focus_set()

Enter_0_2 = Button(self, text="Enter", width=10, command=callback2)
Enter_0_2.pack()

def callback2():
self.display_name = tk.Text(self, width=40, height=2)
self.display_name.pack(side="top", fill="both", expand=True)
self.display_name.insert("end", "Let's begin! Exit back to main screen.")


Enter_0 = Button(self, text="Enter", width=10, command=callback)
Enter_0.pack()

最佳答案

在 Button 的 config() 方法中使用 state="disabled" 来禁用小部件。您可以使用 state="normal" 以类似的方式启用已禁用的小部件。

基于您的示例代码的示例:

import Tkinter as tk


class Application(tk.Frame):

def __init__(self, master):
tk.Frame.__init__(self, master)
self.text = tk.Text(self, width=40, height=2)
self.text.pack(side="top", fill="both", expand=True)
self.text.insert("end", "Please enter your name and class. ")

enter_name = tk.Entry(self)
enter_name.pack()
self.enter_0 = tk.Button(self, text="Enter", width=10, command=self.callback)
self.enter_0.pack()

enter_name.focus_set()
self.pack()

def callback(self):
self.display_name = tk.Text(self, width=40, height=2)
self.display_name.pack(side="top", fill="both", expand=True)
self.display_name.insert("end", "Now please enter your tutor group.")
tutor = tk.Entry(self)
tutor.pack()
tutor.focus_set()
self.enter_0.config(state="disabled")

Enter_0_2 = tk.Button(self, text="Enter", width=10, command=self.callback2)
Enter_0_2.pack()

def callback2(self):
self.display_name = tk.Text(self, width=40, height=2)
self.display_name.pack(side="top", fill="both", expand=True)
self.display_name.insert("end", "Let's begin! Exit back to main screen.")

root = tk.Tk()
root.geometry("600x400")
app = Application(root)
app.mainloop()

关于python - 如何验证 tkinter 中的按钮以便该函数只被调用一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20498664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com