为什么我的elif语句不会执行,但我的if语句将在Python中执行?

Kel*_*aya 2 python if-statement tkinter python-2.x

我在python中写了一个简单的年龄验证,如果用户输入的年份少于2000,那就说欢迎.但是,如果用户输入的年份大于2000,则应将其重定向到另一个站点.

我的代码有效,但只执行if语句而不是elif我输入的年份.

这是我的代码:

from Tkinter import *
import webbrowser
import tkMessageBox


url = 'google.com'
root = Tk()
frame = Frame(root, width=100, height=100)
frame.pack()
L1 = Label(root, text = "Month")
L1.pack(side = LEFT)
E1 = Entry(root,bd=5)
E1.pack(side=LEFT)
L2 = Label(root, text = "Day")
L2.pack(side =LEFT)
E2 = Entry(root, bd= 5)
E2.pack(side = LEFT)
L3 = Label(root, text = "Year")
L3.pack(side = LEFT)
E3 = Entry(root, bd = 5)
E3.pack(side = LEFT)

def getdate():
    tkMessageBox.showinfo(title ="Results", message = E1.get() + " "+ E2.get() + " " + E3.get())
    getage()
    root.destroy()
    #tkMessageBox.showinfo(E2.get())
    #tkMessageBox.showinfo(E3.get())
def getage():

    if E3 < 2000:

        tkMessageBox.showinfo(message= "Welcome! ")
    elif E3 > 2000:
        tkMessageBox.showinfo(message="You will be redirected")
        webbrowser.open_new(url)


b1 = Button(root, text = "Submit", width = 25, command = getdate)
b1.pack()


root.mainloop()
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 7

您正在将Entry()对象与整数进行比较.在Python 2中,数字总是其他类型之前排序,因此它们总是注册为更小:

>>> object() > 2000
True
Run Code Online (Sandbox Code Playgroud)

您想从输入框中获取值并在测试之前转换为整数:

entry = int(E3.get())
if entry > 2000:
     # ...
else:
     # ...
Run Code Online (Sandbox Code Playgroud)

elif除非您希望2000完全忽略该值(您的测试仅适用于大于或小于,不等于的数字),否则无需在此处执行此操作.