python 的 calendar 模块日历小程序
off999 2024-11-21 19:14 29 浏览 0 评论
用python的calendar模块写一个日历小程序,其中有时钟,还可以上下查看每个月,每天可以写备注(记事),同时实现窗口中的日期控件跟随窗口的大小变化,同时变大变小
第一个是时钟功能;
时钟功能就是简单的导入time模块,调用系统当前时间,再用 after(1000,function) 来实现1秒更新,再把结果显示到 label中就即可
def Clock(self):
# 用来显示时间标签
self.timeframe = LabelFrame(self,text='Time:',bg='gray',fg='white')
self.timeframe.place(x=10,y=10)
self.timelabel = Label(self.timeframe,bg='gray',width=15,font=('',12,'bold'),fg='white')
self.timelabel.pack()
def update(self):
# 用来更新显示时间
self.timelabel.config(text=time.strftime("%H : %M : %S")) # 更新时间标签的内容
self.after(1000,self.update) # 间隔 1000 毫秒,调用函数第二个是选择年月功能,通过time模块的 localtime().tm_year 与 localtime().tm_mon方法可以获取当前的年、月,calendar模块中的monthcalenndar方法也需要年,月这两个参数,所以这里就可以通过更新年份和月份来更新日历,需要注意的是1月和12月变化要调用 adjustment()方法来实现
def select_date(self): # 显示顶部 xxxx年 xx 月的标签
for widget in self.winfo_children():
widget.destroy()
self.dateframe = Frame(self)
self.dateframe.pack(anchor='center',pady=10)
self.up_label = Label(self.dateframe, text='<', font=('', 14), width=3, height=2)
self.up_label.pack(side='left', padx=10)
self.up_label.bind("<1>", lambda event: self.adjustment('up')) # 这里绑定一下单击事件,触发时月份 -1
datavar = StringVar()
datavar.set(str(self.year) + ' 年 ' + str(self.month) + ' 月')
self.calenderlabel = Label(self.dateframe, textvariable=datavar)
self.calenderlabel.pack(side='left', padx=10)
self.down_label = Label(self.dateframe, text='>', font=('', 14), width=3, height=2)
self.down_label.pack(side='left', padx=10)
self.down_label.bind("<1>", lambda event: self.adjustment('down')) # 这里绑定一下单击事件,触发时月份 +1
def adjustment(self,Side): # 该方法用来判定月份为1、12时,被调用时年份与月份的值
if Side == 'up':
self.month -=1
if self.month == 0:
self.year -= 1
self.month = 12
elif Side == 'down':
self.month += 1
if self.month == 13:
self.year += 1
self.month = 1
self.select_date()
self.Clock()
self.setUI()
self.myplace()
self.bind('<Configure>', self.window_resize)第三个是备注功能:
每个日期做为一个 Key存到字典中,如果有输入备注,更新这个Key的内容,通过label显示内容
def diary(self,event,Num): # 用于显示日期内容
for widget in self.content_cv.winfo_children():
if self.button == widget:
widget.destroy()
if Num == 1:
for day in self.day_label_list:
if day['text']==event.widget['text']:
self.content_label.config(text='{}年{}月{}日'.format(self.year, self.month, day['text']))
self.day_content.config(text=self.data_dict[event.widget['text']],anchor='w')
if self.first_text == True:
self.text.destroy()
self.first_text = False
else:
self.first_text = False
return
elif Num == 2:
if self.first_text == False:
self.text = Text(self.content_cv)
self.text.place(x=15, y=45, relwidth=0.56, relheight=0.6)
self.button = Button(self.content_cv, text='确 认')
self.button.place(relx=0.75, rely=0.5, relwidth=0.1)
self.button.config(command=lambda: self.change_text(event.widget))
self.first_text = True
else:
self.first_text = False
return
def change_text(self,event): # 用于更新日期备注
for item in self.data_dict:
if self.data_dict[item] == event['text']:
if self.text.get(1.0,'end-1c') == '': # 获取所有文本,但不包括最后的换行符
self.data_dict[item] = event['text']
event.config(text=self.data_dict[item])
else:
self.data_dict[item] = self.text.get(1.0,'end')
event.config(text=self.data_dict[item])
self.text.destroy()
self.button.destroy()第四 个是窗口属性(configure):
这里就是把所有需要变化的控件都使用place()方法实例化,因为 place 方法中有 relwidth(父容器宽的百分比为宽),relheight(父容器高的百分比为高),再把这些控件的 place() 放到一个函数(myplace() )中,在每一次主窗口属性变化时,调用该函数,最后就是一个绑定窗口件事的方法 **bind('<Configure>', self.window_resize)**
def setUI(self): # 设置日历主体
self.cv = Canvas(self,bg='snow',bd=1,relief='solid')
self.bind('<Configure>', self.window_resize)
# 设置星期标签
weekdays = ['一', '二', '三', '四', '五', '六', '日']
self.week_label = []
for i, item in enumerate(weekdays): # 创建星期标签,并加入到标签列表中
self.label = Label(self.cv, text=item, font=self.myfont, width=3, height=2)
self.label.place(relx=0.15 + i / 10, rely=0.04, relwidth=0.1, relheight=0.1)
self.week_label.append(self.label)
# 设置日期标签
self.day_label_list = [] # 用于保存生成日期的标签(对象)
for j, week in enumerate(calendar.monthcalendar(self.year, self.month)): # 通过模块获取日历,返回一个矩阵
for index, day in enumerate(week):
if day == 0:
continue
self.day_label = Label(
self.cv, text=day,
font=self.myfont,
anchor='nw',
bd=1,
relief='solid',
)
self.day_label.place(relx=0.15 + index / 10, rely=0.17 + j / 6, relwidth=0.09, relheight=0.15)
# 标签绑定单击事件
self.day_label.bind("<Button-1>", lambda event, Num=1: self.diary(event, Num))
# 定位系统当天日期
if self.year == time.localtime().tm_year and self.month == time.localtime().tm_mon and day == time.localtime().tm_mday:
self.day_label.config(bg='lightblue', fg='red')
# 把生成的日期标签添加到列表中
self.day_label_list.append(self.day_label)
self.data_dict[day] = '{}年{}月{}日'.format(self.year, self.month, day)
self.content_cv = Canvas(self,bg='snow',bd=1,relief='solid')
self.content_label = Label(self.content_cv,text='日期')
self.day_content = Label(self.content_cv,text='')
self.text = Text(self.content_cv)
self.button = Button(self.content_cv,text='确 认')
def myplace(self):
self.cv.place(x=10,y=60,widt=self.width-20,height=self.height//1.6)
self.myfont = ('楷体', self.width//40) # 重置星期的字休,大小
for label in self.week_label:
label.config(font=self.myfont)
self.myfont = ('楷体', self.width // 60) # 重置日期字体,大小
for day_label in self.day_label_list:
day_label.config(font=self.myfont)
self.content_cv.place(x=10,rely=0.73,widt=self.width-20,relheight=0.25)
self.content_label.place(x=15,y=15,relwidth=0.2,relheight=0.15)
self.day_content.place(x=15,y=45,relwidth=0.96,relheight=0.6)
self.day_content.bind('<Double-1>',lambda event,bool=2:self.diary(event,bool))
# 窗口重置
def window_resize(self,event=None):
if event:
if self.winfo_width() == self.width and self.winfo_height() == self.height:
return
if self.first_load:
self.first_load = False
return
self.width = self.winfo_width()
self.height = self.winfo_height()
self.myplace()全部代码
from tkinter import *
import calendar
import time
class time_calender(Tk):
def __init__(self):
super(time_calender, self).__init__()
self.first_load =True
self.first_text = True
self.labelNum = None
self.data_dict = {}
self.width = 600
self.height = 600
self.minsize(self.width,self.height) # 设置窗口最小尺寸
self.geometry('{}x{}'.format(self.width, self.height)) # 设置窗口初始尺寸
self.year = time.localtime().tm_year # 获取当前系统时间的年份
self.month = time.localtime().tm_mon # 获取当前系统时间的月份
self.day = time.localtime().tm_mday # 获取当前系统时间的日期
self.select_date()
self.Clock()
self.update()
self.myfont = ('楷体', 10)
self.setUI()
self.myplace()
self.bind('<Configure>', self.window_resize) # 绑定窗口属性事件,当窗口发生变化时执行
def Clock(self):
# 用来显示时间标签
self.timeframe = LabelFrame(self,text='Time:',bg='gray',fg='white')
self.timeframe.place(x=10,y=10)
self.timelabel = Label(self.timeframe,bg='gray',width=15,font=('',12,'bold'),fg='white')
self.timelabel.pack()
def update(self):
# 用来更新显示时间
self.timelabel.config(text=time.strftime("%H : %M : %S")) # 更新时间标签的内容
self.after(1000,self.update) # 间隔 1000 毫秒,调用函数
def select_date(self): # 显示顶部 xxxx年 xx 月的标签
for widget in self.winfo_children():
widget.destroy()
self.dateframe = Frame(self)
self.dateframe.pack(anchor='center',pady=10)
self.up_label = Label(self.dateframe, text='<', font=('', 14), width=3, height=2)
self.up_label.pack(side='left', padx=10)
self.up_label.bind("<1>", lambda event: self.adjustment('up')) # 这里绑定一下单击事件,触发时月份 -1
datavar = StringVar()
datavar.set(str(self.year) + ' 年 ' + str(self.month) + ' 月')
self.calenderlabel = Label(self.dateframe, textvariable=datavar)
self.calenderlabel.pack(side='left', padx=10)
self.down_label = Label(self.dateframe, text='>', font=('', 14), width=3, height=2)
self.down_label.pack(side='left', padx=10)
self.down_label.bind("<1>", lambda event: self.adjustment('down')) # 这里绑定一下单击事件,触发时月份 +1
def adjustment(self,Side): # 该方法用来判定月份为1、12时,被调用时年份与月份的值
if Side == 'up':
self.month -=1
if self.month == 0:
self.year -= 1
self.month = 12
elif Side == 'down':
self.month += 1
if self.month == 13:
self.year += 1
self.month = 1
self.select_date()
self.Clock()
self.setUI()
self.myplace()
self.bind('<Configure>', self.window_resize)
def setUI(self): # 设置日历主体
self.cv = Canvas(self,bg='snow',bd=1,relief='solid')
self.bind('<Configure>', self.window_resize)
# 设置星期标签
weekdays = ['一', '二', '三', '四', '五', '六', '日']
self.week_label = []
for i, item in enumerate(weekdays): # 创建星期标签,并加入到标签列表中
self.label = Label(self.cv, text=item, font=self.myfont, width=3, height=2)
self.label.place(relx=0.15 + i / 10, rely=0.04, relwidth=0.1, relheight=0.1)
self.week_label.append(self.label)
# 设置日期标签
self.day_label_list = [] # 用于保存生成日期的标签(对象)
for j, week in enumerate(calendar.monthcalendar(self.year, self.month)): # 通过模块获取日历,返回一个矩阵
for index, day in enumerate(week):
if day == 0:
continue
self.day_label = Label(
self.cv, text=day,
font=self.myfont,
anchor='nw',
bd=1,
relief='solid',
)
self.day_label.place(relx=0.15 + index / 10, rely=0.17 + j / 6, relwidth=0.09, relheight=0.15)
# 标签绑定单击事件
self.day_label.bind("<Button-1>", lambda event, Num=1: self.diary(event, Num))
# 定位系统当天日期
if self.year == time.localtime().tm_year and self.month == time.localtime().tm_mon and day == time.localtime().tm_mday:
self.day_label.config(bg='lightblue', fg='red')
# 把生成的日期标签添加到列表中
self.day_label_list.append(self.day_label)
self.data_dict[day] = '{}年{}月{}日'.format(self.year, self.month, day)
self.content_cv = Canvas(self,bg='snow',bd=1,relief='solid')
self.content_label = Label(self.content_cv,text='日期')
self.day_content = Label(self.content_cv,text='')
self.text = Text(self.content_cv)
self.button = Button(self.content_cv,text='确 认')
def myplace(self):
self.cv.place(x=10,y=60,widt=self.width-20,height=self.height//1.6)
self.myfont = ('楷体', self.width//40) # 重置星期的字休,大小
for label in self.week_label:
label.config(font=self.myfont)
self.myfont = ('楷体', self.width // 60) # 重置日期字体,大小
for day_label in self.day_label_list:
day_label.config(font=self.myfont)
self.content_cv.place(x=10,rely=0.73,widt=self.width-20,relheight=0.25)
self.content_label.place(x=15,y=15,relwidth=0.2,relheight=0.15)
self.day_content.place(x=15,y=45,relwidth=0.96,relheight=0.6)
self.day_content.bind('<Double-1>',lambda event,bool=2:self.diary(event,bool))
# 窗口重置
def window_resize(self,event=None):
if event:
if self.winfo_width() == self.width and self.winfo_height() == self.height:
return
if self.first_load:
self.first_load = False
return
self.width = self.winfo_width()
self.height = self.winfo_height()
self.myplace()
def diary(self,event,Num): # 用于显示日期内容
for widget in self.content_cv.winfo_children():
if self.button == widget:
widget.destroy()
if Num == 1:
for day in self.day_label_list:
if day['text']==event.widget['text']:
self.content_label.config(text='{}年{}月{}日'.format(self.year, self.month, day['text']))
self.day_content.config(text=self.data_dict[event.widget['text']],anchor='w')
if self.first_text == True:
self.text.destroy()
self.first_text = False
else:
self.first_text = False
return
elif Num == 2:
if self.first_text == False:
self.text = Text(self.content_cv)
self.text.place(x=15, y=45, relwidth=0.56, relheight=0.6)
self.button = Button(self.content_cv, text='确 认')
self.button.place(relx=0.75, rely=0.5, relwidth=0.1)
self.button.config(command=lambda: self.change_text(event.widget))
self.first_text = True
else:
self.first_text = False
return
def change_text(self,event): # 用于更新日期备注
for item in self.data_dict:
if self.data_dict[item] == event['text']:
if self.text.get(1.0,'end-1c') == '': # 获取所有文本,但不包括最后的换行符
self.data_dict[item] = event['text']
event.config(text=self.data_dict[item])
else:
self.data_dict[item] = self.text.get(1.0,'end')
event.config(text=self.data_dict[item])
self.text.destroy()
self.button.destroy()
if __name__ == '__main__':
app = time_calender()
app.mainloop()相关推荐
- 笔记本电脑选哪个品牌比较好
-
1、苹果APPLE/美国2、戴尔DELL/美国3、华为HUAWEI/中国4、小米MI/中国5、微软Microsoft/美国6、联想LENOVO/中国7、惠普HP/美国8、华硕ASUS/...
- 10系列显卡排名(10系显卡性能排行)
-
十系显卡指NVIDIAGeForce10系列,是英伟达研发并推出的图形处理器系列,被用以取代NVIDIAGeForce900系列图形处理器。新系列采用帕斯卡微架构来代替之前的麦克斯韦微架构,并...
-
- 最新win7系统下载(windows7最新版本下载)
-
最简单的方法就是,下载完镜像文件后,直接把镜像文件解压,解压到非C盘,然后在解压文件里面找到setup.exe,点击运行即可。安装系统完成后,在C盘找到一个Windows.old(好几个GB,是旧系统打包在这里,垃圾文件了)删除即可。扩展资...
-
2026-01-15 06:43 off999
- 哪个电脑管家软件好用(哪个电脑管家好用些)
-
腾讯电脑管家吧,因为这个是杀毒和管理合一的,占用内存小,因此显得更为简洁,使电脑运行更加流畅此外电脑诊所,工具箱以及4+1的杀毒模式让腾讯电脑管家也收到了广泛的关注4+1杀毒引擎,管家反病毒引擎、金山...
- 怎么进入win7安全模式(怎么进入win7安全模式界面)
-
方法如下:1、首先进入Win7系统,然后使用Win键+R组合键打开运行框,输入“Msconfig”回车进入系统配置。2、在打开的系统配置中,找到“引导”选项,然后单击,选择Win7的引导项,然后在“安...
- 怎么分区固态硬盘(怎样分区固态硬盘)
-
固态硬盘的分区方法与传统机械硬盘基本相同,以下是一个简单的步骤:1.打开磁盘管理工具:在Windows操作系统中,按下Win+X键,选择"磁盘管理"。或者打开控制面板,在"...
-
- 笔记本声卡驱动怎么下载(笔记本如何下载声卡)
-
1、在浏览器中输入并搜索,然后下载并安装。2、安装完成后打开360驱动大师,它就会自动检测你的电脑需要安装或升级的驱动。3、检测完毕后,我们可以看到我们的声卡驱动需要安装或升级,点击安装或升级,就会开始自动安装或升级声卡了。4、升级过程中会...
-
2026-01-15 05:43 off999
- win10加快开机启动速度(加快开机速度 win10)
-
一、启用快速启动功能1.按win+r键调出“运行”在输入框输入“gpedit.msc”按回车调出“组策略编辑器”?2.在“本地组策略编辑器”依次打开“计算机配置——管理模块——系统——关机”在右侧...
-
- excel的快捷键一览表(excel的快捷键一览表超全)
-
Excel快捷键大全的一些操作如下我在工作中经常使用诸如word或Excel之类的办公软件。我相信每个人都不太熟悉这些办公软件的快捷键。使用快捷键将提高办公效率,并使您的工作更加轻松快捷。。例如,在复制时,请使用CtrI+C进行复制,...
-
2026-01-15 05:03 off999
- 华硕u盘启动按f几(华硕u盘装系统按f几进入)
-
F8。1、开机的同时按F8进入BIOS。2、在Boot菜单中,置secure为disabled。3、BootListOption置为UEFI。4、在1stBootPriority中usb—HD...
- 手机云电脑怎么用(手机云端电脑)
-
使用手机云电脑,您首先需要安装相应的云电脑应用。例如,华为云电脑APP。在安装并打开应用后,您将看到一个显示器的图标,这就是您的云电脑。点击这个图标,您将被连接到一个预装有Windows操作系统和必要...
- ie11浏览器怎么安装(ie11浏览器安装步骤)
-
如果IE浏览器11版本你发现无法正常安装,那么很可能是这样几个原因,一个就是电脑的存储空间不够到时无法安装,再有就是网络的问题,如果没有办法安装的话就不要再安装了,本身这个IE浏览器并不是多好用,你最...
- 台式机重装系统win7(台式机怎么重装win7)
-
下面主要介绍两种方法以重装系统:一、U盘重装系统准备:一台正常开机的电脑和一个U盘1、百度下载“U大师”(老毛桃、大白菜也可以),把这个软件下载并安装在电脑上。2、插上U盘,选择一键制作U盘启动(制作...
- 字母下划线怎么打出来(字母下的下划线怎么去不掉)
-
第一步,在电脑上找到文字处理软件WPS,双击即自动新建一个新文档。第二步,在文档录入需要处理的字母和数字,双击鼠标或拖动鼠标选择要处理的内容。第三步,在页面的左上方的横向菜单栏,找到字母U的按纽,点击...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
python入门到脱坑 输入与输出—str()函数
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
失业程序员复习python笔记——条件与循环
-
系统u盘安装(win11系统u盘安装)
-
Python 批量卸载关联包 pip-autoremove
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
