这里我有一个程序,它首先显示一条信息消息,然后你点击下一步,它告诉你在打开主窗口之前输入你的名字
信息->;(下一步)输入名称->;(下)
当我在输入框中输入我的名字时,我想检查它是否不包含1.数字,2.是否为空。在validate=“key”选项下,这意味着一旦我开始键入,它将进行验证。但我希望它只在我按下下一步按钮时检查名称。。。如果没有,它将打开errorbox()
class errorbox():
def __init__(self):
windowError = Tk()
windowError.title("Error")
windowError.geometry('300x400')
error_message = Label(windowError, font=("Bold", 10), justify="left", text="Please enter a valid name")
def clicked1():
description.configure(text="Please enter your name")
nameBox = Entry(windowSplash, width=20, textvariable=name)
nameBox.place(rely=0.5, x=130, anchor=W)
reg = windowSplash.register(validate)
nameBox.config(validate="none",validatecommand=clicked2)
button2 = Button(text="Next", bg="white", width=5, command=lambda:[clicked2(),validate()])
button2.place(rely=0.5, x=300, anchor=E)
button1.destroy()
def validate(input):
if input.isdigit():
print("Invalid name was entered" + input)
errorbox()
return False
elif input is "":
print("No name entered")
errorbox()
return False
else:
return True
def clicked2():
print(name.get(), "logged in...")
windowSplash.destroy()
windowTool = Tk()
windowTool.title("Radial Measurements Calculator Tool")
windowTool.geometry('300x400')
name = StringVar()
windowSplash.mainloop()
欢迎来到Stack Overflow社区
我可能已经解释了你的问题,但请确保下次你提问时提供minimal, reproducible example
以下是我观察到的几件事
validate
函数将input
作为参数,因此请确保通过lambda input = name.get(): [clicked2(),validate(input)]
在lambda函数中传递该参数李>input.isdigit()
并不能保证字符后面/之间可能没有数字,因此我建议您遍历字符串并检查isdigit()
/type()
或使用re
模块。此外,检查空字符串的有效方法可以是if not name.get():
李>validate
函数调用clicked2
,而不是形成next
按钮,因为在这种情况下,您的返回表单validate
不用于任何用途李>相关问题 更多 >
编程相关推荐