Why won't my elif statement execute but my if statement will in Python?
You are comparing the Entry()
object to an integer. In Python 2, numbers are always sorted before other types, so they always register as smaller:
>>> object() > 2000True
You want to get the value from the entry box and convert to an integer before testing:
entry = int(E3.get())if entry > 2000: # ...else: # ...
There is no need to do elif
here, unless you want the value 2000
to be ignored altogether (your test only works for numbers either greater than, or smaller than, not equal).
Entry
is the object type. You want to call the get()
function. Like this:
EntryBox = Entry(root, bd = 5)E3 = int(EntryBox.get())
Also, note that the elif
is completely unnecessary. Just use else
instead.
if E3 < 2000: tkMessageBox.showinfo(message= "Welcome! ")else: tkMessageBox.showinfo(message="You will be redirected") webbrowser.open_new(url)
You compare an Entry
object to an integer
,the if
statement will never be reached because integers
are always sorted before objects
. Look:
>>> a = ['0', 9999, {}, [], False, (), object()]>>> sorted(a)[False, 9999, {}, [], <object object at 0xb74fa558>, '0', ()]>>>
Use get
method to get text from Entry
then convert it with int
:
if int(E3.get()) < 2000:
You can use IntVar()
if you want to get integer from Entry without converting:
...v = IntVar()E3 = Entry(root, bd = 5, textvariable=v)...if E3.get() < 2000: tkMessageBox.showinfo(message= "Welcome! ")else: tkMessageBox.showinfo(message="You will be redirected") webbrowser.open_new(url)