这里是上课用的例子。
import Tkinter as tk
#import tkinter as tk (Python 3.4)
class App:
#Initialization
def __init__(self, window):
#Set the var type for your entry
self.entry_var = tk.StringVar()
self.entry_widget = tk.Entry(window, textvariable=self.entry_var).pack()
self.button = tk.Button(window, text='Test', command=self.check).pack()
def check(self):
#Retrieve the value from the entry and store it to a variable
var = self.entry_var.get()
if var == '':
print "The value is not valid"
else:
print "The value is valid"
root = tk.Tk()
obj = App(root)
root.mainloop()
然后从上面的条目只能取数字和字符串。如果用户输入一个空格,将输出一条错误消息。现在,如果迟到希望你的输入是一个整数或浮点形式或任何其他形式,你只需要抛出!
Example:
yourVar = '5'
newVar = float(yourVar)
>>> 5.0
希望有帮助!