当我尝试一个数字时,它给了我invalid
,但我只想说它invalid
是负数还是不是数字:
from Tkinter import *
import tkMessageBox
class MyApp(object):
def __init__(self):
self.root = Tk()
self.root.wm_title("Question 7")
self.label = Label(self.root, text="Enter weight in pounds",
font=('Calibri', 50))
self.label.pack(padx=20,pady=10)
self.labeltext = StringVar()
self.labeltext.set("")
Label(self.root, textvariable=self.labeltext).pack()
self.entrytext = StringVar()
Entry(self.root, textvariable=self.entrytext).pack()
self.entrytext.trace('w', self.entry_changed)
self.root.mainloop()
def entry_changed(self, a, b, c):
s = self.entrytext.get()
try:
a=int(s)*4.3
self.labeltext.set(a)
except:
if s=="":
self.labeltext.set("")
else:
self.labeltext.set("invalid")
MyApp()