验证tkinter输入框

时间:2014-03-20 15:54:46

标签: python tkinter

嗨,这是我目前的代码

def Validate():
    if Entry1.get() == ('blue') or Entry1.get() ==  ('green') or Entry1.get() == ('brown'):
        window2=Tk()
        label2= Label(window2,text = 'Successful')
        label2.pack()
    else:
        window3=Tk()
        label3 = Label(window3,text = 'Failed')
        label3.pack()

Entry1 = Entry(window)
Entry1.pack()


label1 = Label(window, text = "Please enter colour")
label1.pack()

submitbutton = Button(window, text = "Submit")
submitbutton.pack()

submitbutton.configure(command=Validate)

这有效,但我想试着这样做,以便在输入'blue''Blue''BLUE'时仍然可以成功,有任何想法吗?

我在代码的其他方面使用NOCASE来处理数据库,但我不认为它适用于这种情况

1 个答案:

答案 0 :(得分:1)

您可以简化Validate,尝试:

def Validate():
    if Entry1.get().lower() in ('blue', 'green', 'brown'):
        message = "Successful"
    else:
        message = "Failed"
    window = Tk()
    label = Label(window, text=message)
    label.pack()

这会将文本小写,以便与可接受的值进行比较。