Python Tkinter 输入日期相减
我一直在尝试让我的Tkinter对话框做一个简单的日期减法,但总是遇到问题。日期减法在命令行运行时没问题,但在Tkinter中却没有任何反应。
这段代码是从我另一个成功运行的脚本中借来的,那个脚本的表单和窗口设置都没问题。
#!/usr/bin/python
from Tkinter import *
import datetime
import math
fields = ('Enter Date (mm/dd/yy)', 'Days Since 10/30/14')
def Calc(entries):
d = raw_input(entries['Enter Date (mm/dd/yy)'].get())
nd = datetime.datetime.strptime(d, "%m/%d/%y").date()
d1 = "10/30/14"
od = datetime.datetime.strptime(d1, "%m/%d/%y").date()
diff = (nd - od).days
diff = ('%8.2f' % diff).strip()
entries['Days Since 10/30/14'].delete(0,END)
entries['Days Since 10/30/14'].insert(0, diff)
def makeform(root, fields):
root.title('Date Difference')
entries = {}
for field in fields:
row = Frame(root)
lab = Label(row, width=22, text=field+": ", anchor='w', font=('arial', 12))
ent = Entry(row, font=('arial', 12))
row.pack(side=TOP, fill=X, padx=5, pady=5)
lab.pack(side=LEFT, padx=10)
ent.pack(side=RIGHT, expand=YES, fill=X)
entries[field] = ent
return entries
if __name__ == '__main__':
root = Tk()
root.geometry("400x400+300+300")
ents = makeform(root, fields)
root.bind('<Return>', (lambda event, e=ents: Calc(e)))
b1 = Button(root, text='Calculate', font=('arial',12), command=(lambda e=ents: Calc(e)))
b1.pack(side=LEFT, padx=5, pady=5)
b2 = Button(root, text='Quit', font=('arial',12), command=root.quit)
b2.pack(side=LEFT, padx=5, pady=5)
root.mainloop()
希望能得到一些帮助……
现在我搞得更糟了,怎么都启动不了,还在makeform(root,fields):
这一行把“lab”变量标红了,刚才还可以运行,只是无法进行计算……
有人能帮帮我吗?
1 个回答
1
d = raw_input(entries['Enter Date (mm/dd/yy)'].get())
d = entries['Enter Date (mm/dd/yy)'].get()
raw_input
是通过命令行从用户那里获取数据的。这是故意这样设计的吗?如果你只是想知道用户在输入框里输入了什么,其实不需要用这个。