python日记本程序
时间: 2023-06-23 14:49:16 浏览: 83
好的,以下是一个简单的Python日记本程序,包括了新建、打开、关闭、保存和另存为等文件操作功能,以及日记编辑功能和附加信息的显示功能。同时,程序还可以为日记本设置密码,保护日记的隐私。
```python
import tkinter as tk
import tkinter.filedialog as filedialog
import tkinter.messagebox as messagebox
import datetime
import requests
from cryptography.fernet import Fernet
class Diary:
def __init__(self, password=None):
self.password = password
self.title = ''
self.content = ''
self.additional_info = ''
def set_password(self, password):
self.password = password
def save(self, file_path):
if self.password:
key = Fernet.generate_key()
cipher_suite = Fernet(key)
self.title = cipher_suite.encrypt(self.title.encode())
self.content = cipher_suite.encrypt(self.content.encode())
self.additional_info = cipher_suite.encrypt(self.additional_info.encode())
with open(file_path, 'wb') as f:
f.write(key)
f.write(self.title)
f.write(self.content)
f.write(self.additional_info)
else:
with open(file_path, 'w') as f:
f.write(self.title)
f.write(self.content)
f.write(self.additional_info)
def open(self, file_path):
if self.password:
with open(file_path, 'rb') as f:
key = f.read(32)
cipher_suite = Fernet(key)
self.title = cipher_suite.decrypt(f.read())
self.content = cipher_suite.decrypt(f.read())
self.additional_info = cipher_suite.decrypt(f.read())
else:
with open(file_path, 'r') as f:
self.title = f.readline()
self.content = f.readline()
self.additional_info = f.readline()
def close(self):
self.title = ''
self.content = ''
self.additional_info = ''
class DiaryEditor:
def __init__(self):
self.root = tk.Tk()
self.diary = Diary()
self.create_widgets()
def create_widgets(self):
self.root.title('Diary Editor')
self.root.geometry('800x600')
# 菜单栏
menubar = tk.Menu(self.root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label='New', command=self.new_diary)
filemenu.add_command(label='Open', command=self.open_diary)
filemenu.add_command(label='Save', command=self.save_diary)
filemenu.add_command(label='Save As', command=self.save_as_diary)
menubar.add_cascade(label='File', menu=filemenu)
self.root.config(menu=menubar)
# 标题
tk.Label(self.root, text='Title:').pack()
self.title_entry = tk.Entry(self.root)
self.title_entry.pack()
# 内容
tk.Label(self.root, text='Content:').pack()
self.content_text = tk.Text(self.root)
self.content_text.pack()
# 附加信息
tk.Label(self.root, text='Additional Info:').pack()
self.additional_info_text = tk.Text(self.root, height=5)
self.additional_info_text.pack()
# 天气信息
self.weather_label = tk.Label(self.root, text='')
self.weather_label.pack()
# 保存按钮
tk.Button(self.root, text='Save', command=self.save_diary).pack()
def new_diary(self):
self.diary.close()
self.title_entry.delete(0, tk.END)
self.content_text.delete('1.0', tk.END)
self.additional_info_text.delete('1.0', tk.END)
def open_diary(self):
file_path = filedialog.askopenfilename(filetypes=[('Text Files', '*.txt'), ('Encrypted Files', '*.enc')])
if file_path:
password = None
if messagebox.askyesno('Password', 'Do you want to open a password-protected diary?'):
password = messagebox.askstring('Password', 'Please enter the password:')
self.diary.set_password(password)
self.diary.open(file_path)
self.title_entry.delete(0, tk.END)
self.title_entry.insert(0, self.diary.title.decode())
self.content_text.delete('1.0', tk.END)
self.content_text.insert('1.0', self.diary.content.decode())
self.additional_info_text.delete('1.0', tk.END)
self.additional_info_text.insert('1.0', self.diary.additional_info.decode())
def save_diary(self):
if not self.title_entry.get():
messagebox.showerror('Error', 'Title cannot be empty!')
return
self.diary.title = self.title_entry.get()
self.diary.content = self.content_text.get('1.0', tk.END)
self.diary.additional_info = self.additional_info_text.get('1.0', tk.END)
if self.diary.password:
file_path = filedialog.asksaveasfilename(filetypes=[('Encrypted Files', '*.enc')])
else:
file_path = filedialog.asksaveasfilename(filetypes=[('Text Files', '*.txt')])
if file_path:
self.diary.save(file_path)
messagebox.showinfo('Info', 'Diary saved successfully!')
def save_as_diary(self):
if not self.title_entry.get():
messagebox.showerror('Error', 'Title cannot be empty!')
return
self.diary.title = self.title_entry.get()
self.diary.content = self.content_text.get('1.0', tk.END)
self.diary.additional_info = self.additional_info_text.get('1.0', tk.END)
if self.diary.password:
file_path = filedialog.asksaveasfilename(filetypes=[('Encrypted Files', '*.enc')])
else:
file_path = filedialog.asksaveasfilename(filetypes=[('Text Files', '*.txt')])
if file_path:
self.diary.save(file_path)
messagebox.showinfo('Info', 'Diary saved successfully!')
def update_weather_info(self):
try:
response = requests.get('https://api.seniverse.com/v3/weather/now.json', params={
'key': 'your_key',
'location': 'your_location',
'language': 'zh-Hans',
'unit': 'c'
}, timeout=1)
json_data = response.json()
weather = json_data['results'][0]['now']['text']
temperature = json_data['results'][0]['now']['temperature']
self.weather_label.config(text='Weather: {} {}°C'.format(weather, temperature))
except:
self.weather_label.config(text='Failed to get weather information')
def run(self):
# 更新天气信息
self.update_weather_info()
# 更新附加信息中的时间信息
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.additional_info_text.insert(tk.END, 'Time: {}\n'.format(current_time))
# 运行程序
self.root.mainloop()
if __name__ == '__main__':
editor = DiaryEditor()
editor.run()
```
在这个程序中,我们使用了`tkinter`库实现了GUI界面,并使用了`filedialog`和`messagebox`模块来实现文件操作和提示框功能。程序中的`Diary`类代表一个日记,包括了标题、内容和附加信息等属性,同时可以使用`set_password()`、`save()`和`open()`方法来设置密码、保存和打开日记。在程序中的`DiaryEditor`类中,我们创建了GUI界面,并实现了各种按钮的响应函数,包括新建、打开、保存和另存为等文件操作功能,以及日记编辑功能和附加信息的显示功能。在程序中,我们还使用了`requests`库来获取天气信息,并使用了`datetime`模块来获取当前时间信息。
需要注意的是,程序中的加密功能使用了`cryptography`库中的`Fernet`,需要先安装该库才能正常运行。同时,程序中的天气信息需要使用自己的API Key和地理位置信息才能获取到正确的天气信息。