您的位置首页  散文评论

日记本文档_日记本文档怎么用

前言记录生活点滴是许多人的心愿,但市面上的日记本工具总缺少个性化的定制空间。

日记本文档_日记本文档怎么用

 

前言记录生活点滴是许多人的心愿,但市面上的日记本工具总缺少个性化的定制空间今天我们将用Python开发一款专属日记本程序,具备以下特色:- 美观的Tkinter窗口界面- 自动显示当前日期和指定城市的天气。

- 护眼模式设计,保护视力- 自动追加保存日记到docx文档一、环境准备安装必要库```bashpip install python-docx requests```获取天气API密钥访问[OpenWeatherMap官网](

https://openweathermap.org/)注册并获取API密钥,将以下URL中的`YOUR_API_KEY`替换为你的密钥:```pythonWEATHER_API = "http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY&units=metric"

二、界面设计(Tkinter实现)1. 创建主窗口框架```pythonimport tkinter as tkfrom tkinter import messageboxfrom tkinter import ttk

初始化窗口root = tk.Tk()root.title("智能日记本")root.geometry("800x600")root.configure(bg="F0FFF0") 护眼背景色```2. 布局模块

```python顶部信息栏info_frame = tk.Frame(root, bg="F0FFF0")info_frame.pack(pady=10)date_label = tk.Label(info_frame, text="日期", font=("Arial", 14), bg="F0FFF0")

date_label.pack(side=tk.LEFT, padx=5)weather_label = tk.Label(info_frame, text="天气", font=("Arial", 14), bg="F0FFF0")

weather_label.pack(side=tk.LEFT, padx=5)日记输入区text_area = tk.Text(root,font=("微软雅黑", 16),bg="FFFACD", 米白色输入框

fg="2F4F4F", 深蓝灰字体wrap=tk.WORD,height=20,width=60)text_area.pack(pady=20, padx=30)保存按钮save_btn = tk.Button(root,

text="保存日记",font=("Arial", 12),command=save_diary,bg="90EE90", 淡绿色按钮activebackground="3CB371")save_btn.pack()

三、核心功能实现1. 实时获取日期和天气```pythonimport datetimeimport requestsdef get_current_date():return datetime.datetime.now().strftime("%Y年%m月%d日")

def get_weather(city="Beijing"):try:response = requests.get(WEATHER_API.format(city=city))data = response.json()

return f"{data[weather][0][description]} | 温度: {data[main][temp]}°C"except:return "天气获取失败"```2. 自动保存日记

```pythonfrom docx import Documentdef save_diary():content = text_area.get("1.0", tk.END).strip()if not content:

messagebox.showwarning("提示", "请输入日记内容")return创建/追加文档doc = Document()try:doc.add_heading(f"日期:{current_date}", 0)

doc.add_paragraph(f"天气:{current_weather}")doc.add_paragraph("")doc.add_paragraph(content)追加模式保存doc.save("日记本.docx")

messagebox.showinfo("成功", "日记已保存")except Exception as e:messagebox.showerror("错误", str(e))四、完整代码整合```python

完整代码(需替换API密钥)import tkinter as tkfrom tkinter import messageboximport datetimeimport requestsfrom docx import Document

配置参数API_KEY = "YOUR_OPENWEATHERMAP_API_KEY"WEATHER_API = f"http://api.openweathermap.org/data/2.5/weather?q={{}}&appid={API_KEY}&units=metric"

def main():root = tk.Tk()root.title("智能日记本")root.geometry("800x600")root.configure(bg="F0FFF0")功能函数def refresh_info():

current_date = datetime.datetime.now().strftime("%Y年%m月%d日")date_label.config(text=current_date)city = city_entry.get() or "Beijing"

weather_info = get_weather(city)weather_label.config(text=weather_info)def get_weather(city):try:response = requests.get(WEATHER_API.format(city))

data = response.json()return f"{data[weather][0][description]} | 温度: {data[main][temp]}°C"except:return "天气获取失败"

def save_diary():content = text_area.get("1.0", tk.END).strip()if not content:messagebox.showwarning("提示", "请输入日记内容")

returncurrent_date = datetime.datetime.now().strftime("%Y年%m月%d日")try:创建新文档或追加内容doc = Document()doc.add_heading(f"日期:{current_date}", 0)

doc.add_paragraph(f"天气:{get_weather()}") 重新获取实时天气doc.add_paragraph("")doc.add_paragraph(content)追加模式保存

doc.save("日记本.docx")messagebox.showinfo("成功", "日记已保存")except Exception as e:messagebox.showerror("错误", str(e))

界面组件info_frame = tk.Frame(root, bg="F0FFF0")info_frame.pack(pady=10)date_label = tk.Label(info_frame, text="", font=("Arial", 14), bg="F0FFF0")

date_label.pack(side=tk.LEFT, padx=5)weather_label = tk.Label(info_frame, text="", font=("Arial", 14), bg="F0FFF0")

weather_label.pack(side=tk.LEFT, padx=5)city_entry = tk.Entry(info_frame, width=15)city_entry.pack(side=tk.LEFT, padx=5)

refresh_btn = tk.Button(info_frame,text="刷新天气",command=lambda: refresh_info(),bg="90EE90")refresh_btn.pack(side=tk.LEFT, padx=5)

text_area = tk.Text(root,font=("微软雅黑", 16),bg="FFFACD",fg="2F4F4F",wrap=tk.WORD,height=20,width=60)text_area.pack(pady=20, padx=30)

save_btn = tk.Button(root,text="保存日记",command=save_diary,bg="90EE90")save_btn.pack()初始化显示refresh_info()

root.mainloop()if __name__ == "__main__":main()五、使用说明1. 运行程序后,输入城市名称点击"刷新天气"获取当地天气2. 在米色输入框中撰写日记,支持换行和段落

3. 点击"保存日记"按钮后,内容会自动追加到`日记本.docx`文件4. 每日重启程序时会自动显示当日日期和最新天气六、扩展建议- 增加"夜间模式"切换按钮(修改背景色)- 添加语音输入功能(使用speech_recognition库)

- 实现多城市天气对比显示- 增加密码保护功能(加密保存日记)通过这个项目,我们不仅实现了基础的日记记录功能,更掌握了Python在GUI开发、API调用和文档处理方面的综合应用让代码真正服务于生活,这样的小工具何乐而不为。

运行效果图如下:

免责声明:本站所有信息均搜集自互联网,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻处理。联系QQ:1640731186