繁体   English   中英

通过单击按钮验证 Python Tkinter 名称条目?

[英]Validate Python Tkinter name entry by clicking button?

所以这里我有一个程序,它首先显示一条信息消息,然后您单击下一步,它会告诉您在打开主窗口之前输入您的姓名。

INFO ->(next) ENTER NAME ->(next)

当我在输入框中输入我的名字时,我希望检查它不包含 1.numbers 和 2.is not empty。 在 validate="key" 选项下,这意味着一旦我开始输入它就会验证。 但我希望它只在我按下 NEXT 按钮后检查名称......如果不是它会打开 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 社区。

我可能已经解释了您的问题,但请确保下次您提出问题时提供一个最小的、可重现的示例

以下是我观察到的几件事。

  1. validate函数将input作为参数,因此请确保通过lambda input = name.get(): [clicked2(),validate(input)]在 lambda 函数中传递它。
  2. 通过检查input.isdigit()并不能保证字符之后/之间可能没有数字,因此我建议您遍历字符串并检查isdigit() / type()或使用re模块。 此外,检查空字符串的有效方法可能是if not name.get():
  3. 如果您打算仅在验证后打开新窗口,我建议您在条件下从validate函数调用clicked2 ,而不是形成next按钮,因为在这种情况下,您的返回表单validate不用于任何事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM