马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 青出于蓝 于 2023-7-19 22:30 编辑
Python Tkinter 实现记事本程序
大家好
这是我的第一篇帖子,找了以前的一个小的tkinter项目包括讲解给大家
教程
1.安装tkinter、pyperclip库pip install tkinter
pip install pyperclip
2.主题窗口部分
记事本,说白了最重要的就是个文本框import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
import pyperclip
root = tk.Tk()
root.title("记事本")
text_box = tk.Text(root)
text_box.pack()
3.菜单栏
这个肯定是不可或缺的 menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
file_menu = tk.Menu(menu_bar, tearoff=False)
menu_bar.add_cascade(label="文件", menu=file_menu)
file_menu.add_command(label="打开", command=open_file)
file_menu.add_command(label="保存", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="退出", command=root.quit)
edit_menu = tk.Menu(menu_bar, tearoff=False)
menu_bar.add_cascade(label="编辑", menu=edit_menu)
edit_menu.add_command(label="粘贴", command=pastes)
help_menu = tk.Menu(menu_bar, tearoff=False)
menu_bar.add_cascade(label="帮助", menu=help_menu)
help_menu.add_command(label="关于", command=about)
4.编写菜单栏中的功能们def open_file():
file_path = tk.filedialog.askopenfilename()
if file_path:
with open(file_path, 'r') as f:
content = f.read()
text_box.delete(1.0, tk.END)
text_box.insert(tk.END, content)
def save_file():
file_path = tkinter.filedialog.asksaveasfilename(defaultextension='test', filetypes=[("文本文档", ".txt"), ("word文件", ".docx")])
if file_path:
content = text_box.get(1.0, tk.END)
with open(file_path, 'w') as f:
f.write(content)
def about():
tk.messagebox.showinfo("关于", "本产品由AbyssKing制作")
def pastes():
text_box.insert(tk.END, pyperclip.paste())
5.别忘了让tkinter跑起来呀
|