|

楼主 |
发表于 2018-2-6 14:20:08
|
显示全部楼层
本帖最后由 syyfonline 于 2018-2-6 14:21 编辑
问题已经自己解决了,谢谢大家 代码给大家分享:
- import calendar as cal
- import tkinter as tk
- import time
- app = tk.Tk()
- app.title('Syyf-Office')
- #获取当前时间time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=28, tm_sec=49, tm_wday=3, tm_yday=98, tm_isdst=0)
- localtime = time.localtime(time.time())
- tm_year = localtime.tm_year
- tm_mon = localtime.tm_mon
- tm_mday = localtime.tm_mday
- class Li:
- def __init__(self,year,mon,day):
- self.year = year
- self.mon = mon
- self.day = day
- self.display(self.year,self.mon,self.day)
- def current_time(self):
- self.localtime = time.localtime(time.time())
- self.y = self.localtime.tm_year
- self.m = self.localtime.tm_mon
- self.d = self.localtime.tm_mday
- self.display(self.y, self.m, self.d)
- def add_time(self):
- self.y = self.dy
- self.m = self.dm
- self.d = self.day
- self.m += 1
- if self.m <= 12:
- self.display(self.y, self.m,self.d)
- else:
- self.y += 1
- self.m = 1
- self.display(self.y, self.m,self.d)
- def del_time(self):
- self.y = self.dy
- self.m = self.dm
- self.d = self.day
- self.m -= 1
- if self.m >=1 :
- self.display(self.y, self.m,self.d)
- else:
- self.y -= 1
- self.m = 12
- self.display(self.y, self.m,self.d)
- def display(self,y,m,d):
- self.dy = y #存储当前选择的年
- self.dm = m #存储当前选择的月
- self.day = d #存储当前天
- datas = cal.monthrange(y,m)
- xq = datas[0] # 当前月从星期几开始
- ts = datas[1] # 当前月一共多少天
- h = 0 # 从第0行开始
- # 显示当前时间
- butt = tk.Button(app, text="当前时间:%d年%d月%d日" % (self.year,self.mon,self.day), width=5 * 9,command=self.current_time).grid(
- row=h, column=0, columnspan=7)
- h += 1
- # 显示当前年+、-操作
- butt = tk.Button(app, text="<", width=5,command=self.del_time).grid(row=h, column=0)
- label = tk.Label(app, text=str(y)+"年"+str(m)+"月", width=5 * 6).grid(row=h, column=0, columnspan=7)
- butt = tk.Button(app, text=">", width=5,command=self.add_time).grid(row=h, column=6)
- h += 1
- # 显示星期
- weeks = ("星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日")
- columns = 0
- for week in weeks:
- butt = tk.Label(app, text=week, width=5).grid(row=h, column=columns)
- columns += 1
- h += 1
- c = 0 # 从第0列开始
- x = xq # 获得每个月从星期几开始
- # 填充每个月没有的星期的天数
- while x != 0:
- butt = tk.Label(app, text=" ", width=5).grid(row=h, column=c)
- c += 1
- x -= 1
- cc = xq - 1
- # 输出日历内容
- for j in range(1, ts + 1):
- xq += 1
- cc += 1
- if xq % 7 == 0:
- butt = tk.Button(app, text=j, width=5).grid(row=h, column=cc)
- h += 1
- cc = -1
- else:
- butt = tk.Button(app, text=j, width=5).grid(row=h, column=cc)
- dd = Li(tm_year,tm_mon,tm_mday)
- app.mainloop()
复制代码 |
|