python09

完成一个综合项目,结合所学知识实践编写一个小型应用程序 :

import tkinter as tk
from tkinter import simpledialog, messagebox
from tkinter import ttk
from datetime import datetime
import re
import lunardate
from tkinter import PhotoImage

APP_ICON_PATH = 'img.png'
# 设置主题颜色和字体
BG_COLOR = "#F0F0F0"
BTN_BG = "#ADD8E6"
BTN_FG = "black"
BTN_HOVER_BG = "#87CEFA"
FONT_STYLE = ("Helvetica", 12)

DATE_PATTERN = re.compile(r'^\d{4}-\d{2}-\d{2}$')

CHINA_HOLIDAYS = {
datetime(year=2024, month=1, day=1): "元旦",
datetime(year=2024, month=5, day=1): "劳动节",
datetime(year=2024, month=10, day=1): "国庆节",
datetime(year=2024, month=1, day=22): "春节",
datetime(year=2024, month=4, day=4): "清明节",
datetime(year=2024, month=5, day=5): "端午节",
datetime(year=2024, month=8, day=15): "中秋节",
}


class CalendarApp:
def __init__(self, root):
self.root = root
self.root.title("万年历")
self.root.iconbitmap(APP_ICON_PATH) # 设置窗口图标
self.root.configure(bg=BG_COLOR)
self.root.geometry("400x300")
self.root.resizable(True, True) # 允许窗口大小调整
self.current_date = datetime.now()
self.create_widgets()

def create_widgets(self):
self.root.columnconfigure(0, weight=1) # 让第一列自适应宽度
self.root.rowconfigure(0, weight=1) # 让第一行自适应高度

# 设置窗口图标
self.root.iconbitmap(APP_ICON_PATH)

# 日期显示标签
self.date_label = ttk.Label(self.root, text=self.current_date.strftime("%Y-%m-%d %A"), font=FONT_STYLE,
background=BG_COLOR)
self.date_label.grid(row=0, column=0, pady=(20, 10), padx=20, sticky='EW') # 使用grid布局并让标签内容水平填充

# 修改时间按钮
self.modify_time_button = ttk.Button(self.root, text="修改当前时间", command=self.modify_time, style="TButton")
self.modify_time_button.grid(row=1, column=0, pady=5, sticky='EW')

# 计算日期差按钮
self.date_difference_button = ttk.Button(self.root, text="计算日期差", command=self.calculate_date_difference,
style="TButton")
self.date_difference_button.grid(row=2, column=0, pady=5, sticky='EW')

# 节假日查询按钮
self.holiday_query_button = ttk.Button(self.root, text="节假日查询", command=self.query_holiday,
style="TButton")
self.holiday_query_button.grid(row=3, column=0, pady=5, sticky='EW')

# 农历查询按钮
self.lunar_query_button = ttk.Button(self.root, text="农历查询", command=self.query_lunar_date, style="TButton")
self.lunar_query_button.grid(row=4, column=0, pady=(5, 20), sticky='EW')



def validate_date_input(self, date_str):

return bool(DATE_PATTERN.match(date_str))

def modify_time(self):
top = tk.Toplevel(self.root)
top.title("填入新日期")

label = tk.Label(top, text="请填入新日期例如(YYYY-MM-DD):")
label.pack(pady=5)

entry = tk.Entry(top)
entry.pack(pady=5)

def confirm_date():
new_date = entry.get()
if self.validate_date_input(new_date):
try:
self.current_date = datetime.strptime(new_date, "%Y-%m-%d")
self.update_date_label()
top.destroy()
except ValueError as e:
messagebox.showerror("错误", f"日期转换错误: {e}")
else:
messagebox.showerror("错误", "日期格式无效。请以 YYYY-MM-DD 格式输入日期。")

confirm_button = tk.Button(top, text="确认", command=confirm_date)
confirm_button.pack(pady=5)

def calculate_date_difference(self):
self.new_date = tk.simpledialog.askstring("输入", "输入另一个日期以计算差额(YYYY-MM-DD):")
try:
other_date = datetime.strptime(self.new_date, "%Y-%m-%d")
date_difference = abs((self.current_date - other_date).days)
messagebox.showinfo("日期差", f"日期之间的差异: {date_difference} 天.")
except ValueError:
messagebox.showerror("错误", "日期格式无效。请以 YYYY-MM-DD 格式输入日期。")

def query_holiday(self):
today = datetime(year=self.current_date.year, month=self.current_date.month, day=self.current_date.day)

if today in CHINA_HOLIDAYS:
holiday_name = CHINA_HOLIDAYS[today]
messagebox.showinfo("假期", f"今天是 {holiday_name}!")
else:
# 判断是否为周末(星期六或星期日)
if today.weekday() in [5, 6]:
messagebox.showinfo("假期", "今天是周末,但不是假期。")
else:
messagebox.showinfo("假期", "今天不是假期或周末。")

def query_lunar_date(self):
lunar_date = lunardate.LunarDate.fromSolarDate(self.current_date.year, self.current_date.month, self.current_date.day)
messagebox.showinfo("农历日期", f"{lunar_date.year}年{lunar_date.month}月{lunar_date.day}日")

def update_date_label(self):
self.date_label.config(text=self.current_date.strftime("%Y-%m-%d %A"))

if __name__ == "__main__":
root = tk.Tk()
app = CalendarApp(root)
root.mainloop()
posted @ 2024-05-15 23:27  芊羽鱼  阅读(12)  评论(0)    收藏  举报