小小万年历改头换面了
前阶段编写了一个[万年历小程序][ https://jl-zhenlaixiaowei.blog.csdn.net/article/details/139180401?spm=1001.2014.3001.5502]
因为界面不太友好,所以这两天有空闲时间,我又对小工具进行了功能增加并改成了窗口界面,可以用鼠标点击和键盘输入年月日。其中界面的设计大部分是用AI实现的,又一次切实感受到了AI时代的高效、快捷。并且把这个小工具打包成exe文件,分享给身边的好朋友。
源码如下:
import tkinter as tk
from tkinter import messagebox
from zhdate import ZhDate
from datetime import datetime
from lunardate import LunarDate
class DateConverterGUI:
def __init__(self, root):
self.root = root
self.root.title("小小万年历--微信公众号强壮Python")
self.root.geometry("400x250")
self.root.resizable(False, False)
# 创建标签和输入框
self.label = tk.Label(root, text="今天:{}年{}月{}日".format(datetime.today().year, datetime.today().month,
datetime.today().day))
self.choice_var = tk.IntVar()
self.choice_var.set(1)
self.radio1 = tk.Radiobutton(root, text="阳历转阴历", variable=self.choice_var, value=1)
self.radio2 = tk.Radiobutton(root, text="阴历转阳历", variable=self.choice_var, value=2)
self.year_label = tk.Label(root, text=" 年 份:")
self.year_entry = tk.Entry(root)
self.month_label = tk.Label(root, text=" 月 份:")
self.month_entry = tk.Entry(root)
self.day_label = tk.Label(root, text=" 日 份:")
self.day_entry = tk.Entry(root)
self.convert_button = tk.Button(root, text=" 转换日期 ", command=self.convert_date)
self.label.grid(row=0, column=0, padx=3, sticky=tk.W)
self.year_label.grid(row=1, column=0, padx=32, pady=5, sticky=tk.W)
self.year_entry.grid(row=1, column=1, pady=5, sticky=tk.W)
self.month_label.grid(row=2, column=0, padx=32, pady=5, sticky=tk.W)
self.month_entry.grid(row=2, column=1, pady=5, sticky=tk.W)
self.day_label.grid(row=3, column=0, padx=32, pady=5,sticky=tk.W)
self.day_entry.grid(row=3, column=1, pady=5,sticky=tk.W)
self.year_label.config(highlightcolor='red')
self.radio1.grid(row=5, column=0, padx=7, sticky=tk.W)
self.radio2.grid(row=5, column=1, sticky=tk.W)
# 创建输出结果的文本框
self.result_label = tk.Label(root, text="转换结果:")
self.result_text = tk.Text(root, height=19, width=57)
self.result_text.grid(row=9, column=0, columnspan=5, pady=6, sticky=tk.W)
self.convert_button.grid(row=8, column=0, columnspan=5, padx=284, sticky=tk.W)
def convert_date(self):
# 清空之前的结果
self.result_text.delete('1.0', tk.END)
choice = self.choice_var.get()
try:
year = int(self.year_entry.get())
month = int(self.month_entry.get())
day = int(self.day_entry.get())
except Exception as e:
messagebox.showerror("错误", "年月日不能为空!")
try:
if choice == 1:
solar_date = SolarLunar(year, month, day)
result = solar_date.solar_convert_date()
elif choice == 2:
lunar_date = SolarLunar(year, month, day)
result = lunar_date.lunar_convert_date()
# 将结果显示在文本框中
self.result_text.insert(tk.END, result)
except Exception as e:
messagebox.showerror("错误", "数据转换错误!!请核对日份!!!")
def run(self):
self.root.mainloop()
class SolarLunar:
# 定义初始化函数
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
# 阳历转阴历
def solar_convert_date(self):
self._date = datetime(year=self.year, month=self.month, day=self.day)
chinese_date = ZhDate.from_datetime(self._date).chinese()
weeknum, weekday = self.get_weekday()
result = f"阴历日期(农历):{chinese_date}\n" \
f"阳历:全年的第{weeknum}周的{['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'][weekday - 1]}"
return result
# 阴历转阳历
def lunar_convert_date(self):
lunardate = LunarDate(self.year, self.month, self.day).toSolarDate()
lunardate_list = str(lunardate).split('-')
solar_year, solar_month, solar_day = int(lunardate_list[0]), int(lunardate_list[1]), int(lunardate_list[2])
self._date = datetime(solar_year, solar_month, solar_day)
weeknum, weekday = self.get_weekday()
result = f"阳历日期(阳历):{lunardate}\n" \
f"全年的第{weeknum}周的{['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'][weekday - 1]}"
return result
# 阳历的全年第几周的周几
def get_weekday(self):
"""
Convert date to weekday (Monday=0, Tuesday=1, ..., Sunday=6)
"""
return (self._date.isocalendar().week), (self._date.weekday() + 1) % 7
if __name__ == '__main__':
root = tk.Tk()
app = DateConverterGUI(root)
app.run()
运行效果如下: