Python Tkinter输入日期减法

时间:2014-11-04 20:27:25

标签: python datetime tkinter

我一直试图让我的Tkinter对话框做一个简单的日期减法,但有些事情正在阻止它。从shell运行时,日期减法似乎有所帮助,但我从中得不到任何东西就是Tk。

代码是从我已成功运行所有窗体和窗口设置的另一个脚本借来的。

#!/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()

任何帮助将不胜感激......

即使是现在我也把它搞砸了,不知何故它甚至都没有开始,并突出了实验室&#34; makeform(root,fields):下的变量,它在一分钟前运行,但不会进行计算......

帮助?

1 个答案:

答案 0 :(得分:1)

d = raw_input(entries['Enter Date (mm/dd/yy)'].get())

raw_input通过命令行从用户获取数据。这是故意的吗?如果您只想查找用户在条目中键入的内容,则不需要它。

d = entries['Enter Date (mm/dd/yy)'].get()