现在的位置: 首页 > 综合 > 正文

Python 的格式化时间练习(3)

2011年11月04日 ⁄ 综合 ⁄ 共 3581字 ⁄ 字号 评论关闭

用 Tkinter 把这玩意包装了一下

不过有几个按钮返回值不正常,还没找到解决方法呢

 

12小时制小时数

A.M或.PM的表示

本地相应时间表示

 

贴代码:

fmtTimeTkGui.py

 1 # -*- coding:UTF-8 -*-
 2 
 3 from Tkinter import *
 4 import time
 5 from fmtTimeTkLg import *
 6 
 7 root = Tk()
 8 
 9 e1 = Entry()
10 e2 = Entry()
11 
12 b1  = Button(text = '本地简化星期名称', command = ins_Time1)
13 b2  = Button(text = '本地完整星期名称', command = ins_Time2)
14 b3  = Button(text = '本地简化月份名称', command = ins_Time3)
15 b4  = Button(text = '本地完整月份名称', command = ins_Time4)
16 b5  = Button(text = '两位数的年份表示', command = ins_Time5)
17 b6  = Button(text = '四位数的年份表示', command = ins_Time6)
18 b7  = Button(text = '月(01-12)', command = ins_Time7)
19 b8  = Button(text = '天(0-31)', command = ins_Time8)
20 b9  = Button(text = '24小时制小时数', command = ins_Time9)
21 b10 = Button(text = '12小时制小时数', command = ins_Time10)
22 b11 = Button(text = '分钟数(00-59)', command = ins_Time11)
23 b12 = Button(text = '秒数(00-59)', command = ins_Time12)
24 b13 = Button(text = '日期和时间表示', command = ins_Time13)
25 b14 = Button(text = '年中的第几天', command = ins_Time14)
26 b15 = Button(text = 'A.M或P.M的表示', command = ins_Time15)
27 b16 = Button(text = '年中的第几个星期', command = ins_Time16)
28 b17 = Button(text = '年中的第几个星期', command = ins_Time17)
29 b18 = Button(text = '星期几', command = ins_Time18)
30 b19 = Button(text = '本地相应日期表示', command = ins_Time19)
31 b20 = Button(text = '本地相应时间表示', command = ins_Time20)
32 b21 = Button(text = '当前时区名称', command = ins_Time21)
33 
34 e1.pack(padx = 10, pady = 10, fill = X)
35 e2.pack(padx = 10, pady = 10, fill = X)
36              
37 b1.place(x = 10, y = 80)
38 b2.place(x = 120, y = 80)
39 b3.place(x = 230, y = 80)
40 b4.place(x = 340, y = 80)
41 b5.place(x = 450, y = 80)
42 b6.place(x = 560, y = 80)
43 
44 b7.place(x = 10, y = 115)
45 b8.place(x = 120, y = 115)
46 b9.place(x = 230, y = 115)
47 b10.place(x = 340, y = 115)
48 b11.place(x = 450, y = 115)
49 b12.place(x = 560, y = 115)
50 
51 b13.place(x = 10, y = 150)
52 b14.place(x = 120, y = 150)
53 b15.place(x = 230, y = 150)
54 b16.place(x = 340, y = 150)
55 b17.place(x = 450, y = 150)
56 b18.place(x = 560, y = 150)
57 
58 b19.place(x = 10, y = 185)
59 b20.place(x = 120, y = 185)
60 
61 root.mainloop()
fmtTimeTkLg.py

  1 # -*- coding:UTF-8 -*-
  2 
  3 import time
  4 from Tkinter import *
  5 import fmtTimeTkGui 
  6 
  7 def ins_Now(fmt):
  8     return time.strftime(fmt, time.localtime(time.time()))
  9 
 10 def ins_fmtTime():
 11     return time.strftime('%Y-%m-%d', time.localtime(time.time()))
 12 
 13 def ins_entry(fmt):
 14     gotTime = fmtTimeTkGui.e1.get()
 15     fmtTimeTkGui.e2.delete(0, END)
 16 
 17     if gotTime:
 18         fmtTimeTkGui.e2.insert(0, time.strftime(fmt, time.strptime(gotTime, '%Y-%m-%d')))
 19     else:
 20         fmtTimeTkGui.e1.insert(0, ins_fmtTime())
 21         print ins_fmtTime()
 22         fmtTimeTkGui.e2.insert(0, ins_Now(fmt))
 23         print ins_Now(fmt)
 24 
 25 def ins_entry_time(fmt):
 26     fmtTimeTkGui.e1.delete(0, END)
 27     fmtTimeTkGui.e2.delete(0, END)
 28 
 29     fmtTimeTkGui.e1.insert(0, ins_fmtTime())
 30     fmtTimeTkGui.e2.insert(0, ins_Now(fmt))
 31     
 32 def ins_Time1():
 33     fmt = '%a'
 34     ins_entry(fmt)
 35     print ins_fmtTime()
 36     print ins_Now(fmt)
 37 
 38 
 39 def ins_Time2():
 40     fmt = '%A'
 41     ins_entry(fmt)
 42  
 43 def ins_Time3():
 44     fmt = '%b'
 45     ins_entry(fmt)
 46 
 47 def ins_Time4():
 48     fmt = '%B'
 49     ins_entry(fmt)
 50 
 51 def ins_Time5():
 52     fmt = '%y'
 53     ins_entry(fmt)
 54 
 55 def ins_Time6():
 56     fmt = '%Y'
 57     ins_entry(fmt)
 58 
 59 def ins_Time7():
 60     fmt = '%m'
 61     ins_entry(fmt)
 62 
 63 def ins_Time8():
 64     fmt = '%d'
 65     ins_entry(fmt)
 66 
 67 def ins_Time9():
 68     fmt = '%H'
 69     ins_entry_time(fmt)
 70     print ins_fmtTime()
 71     print ins_Now(fmt)
 72 
 73 def ins_Time10():
 74     fmt = '%I'
 75     ins_entry(fmt)
 76 
 77 def ins_Time11():
 78     fmt = '%M'
 79     ins_entry_time(fmt)
 80 
 81 def ins_Time12():
 82     fmt = '%S'
 83     ins_entry_time(fmt)
 84 
 85 def ins_Time13():
 86     fmt = '%c'
 87     ins_entry_time(fmt)
 88 
 89 def ins_Time14():
 90     fmt = '%j'
 91     ins_entry_time(fmt)
 92 
 93 def ins_Time15():
 94     fmt = '%p'
 95     ins_entry(fmt)
 96 
 97 def ins_Time16():
 98     fmt = '%U'
 99     ins_entry_time(fmt)
100 
101 def ins_Time17():
102     fmt = '%W'
103     ins_entry(fmt)
104 
105 def ins_Time18():
106     fmt = '%w'
107     ins_entry(fmt)
108 
109 def ins_Time19():
110     fmt = '%x'
111     ins_entry(fmt)
112 
113 def ins_Time20():
114     fmt = '%X'
115     ins_entry(fmt)
116     
117 def ins_Time21():
118     fmt = '%Z'
119     ins_entry_time(fmt)

 

运行结果:

 

可以直接下载来看看,地址在这:fmtTimeTkLg.rar

抱歉!评论已关闭.