简单的Python记事本应用程序

下面是一个简单的Python记事本应用程序,使用Tkinter库来创建图形用户界面。这个程序允许用户输入文本并保存到文件中。

首先,确保你已经安装了Python和Tkinter库(Tkinter通常是Python标准库的一部分,所以大多数情况下不需要单独安装)。

以下是完整的代码:

python 复制代码
import tkinter as tk
from tkinter import filedialog, messagebox

class Notepad:
    def __init__(self, root):
        self.root = root
        self.root.title("简易记事本")
        self.root.geometry("600x400")

        # 创建文本框
        self.text_area = tk.Text(self.root, undo=True)
        self.text_area.pack(fill=tk.BOTH, expand=1)

        # 创建菜单栏
        self.menu_bar = tk.Menu(self.root)
        self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
        self.file_menu.add_command(label="新建", command=self.new_file)
        self.file_menu.add_command(label="打开", command=self.open_file)
        self.file_menu.add_command(label="保存", command=self.save_file)
        self.file_menu.add_separator()
        self.file_menu.add_command(label="退出", command=self.exit_app)
        self.menu_bar.add_cascade(label="文件", menu=self.file_menu)

        self.edit_menu = tk.Menu(self.menu_bar, tearoff=0)
        self.edit_menu.add_command(label="撤销", command=self.undo_action)
        self.edit_menu.add_command(label="重做", command=self.redo_action)
        self.menu_bar.add_cascade(label="编辑", menu=self.edit_menu)

        self.root.config(menu=self.menu_bar)

    def new_file(self):
        if messagebox.askokcancel("新建", "是否要清除当前内容?"):
            self.text_area.delete(1.0, tk.END)

    def open_file(self):
        file_path = filedialog.askopenfilename(defaultextension=".txt",
                                              filetypes=[("Text files", "*.txt"),
                                                         ("All files", "*.*")])
        if file_path:
            with open(file_path, "r") as file:
                content = file.read()
                self.text_area.delete(1.0, tk.END)
                self.text_area.insert(tk.END, content)

    def save_file(self):
        file_path = filedialog.asksaveasfilename(defaultextension=".txt",
                                                 filetypes=[("Text files", "*.txt"),
                                                            ("All files", "*.*")])
        if file_path:
            with open(file_path, "w") as file:
                content = self.text_area.get(1.0, tk.END)
                file.write(content)
            messagebox.showinfo("保存成功", f"文件已保存至 {file_path}")

    def exit_app(self):
        if messagebox.askokcancel("退出", "确定要退出吗?"):
            self.root.destroy()

    def undo_action(self):
        try:
            self.text_area.edit_undo()
        except tk.TclError:
            pass

    def redo_action(self):
        try:
            self.text_area.edit_redo()
        except tk.TclError:
            pass

if __name__ == "__main__":
    root = tk.Tk()
    notepad = Notepad(root)
    root.mainloop()

你可以将上述代码复制到一个.py文件中并运行它。这将启动一个简单的记事本应用程序,带有基本的文件操作功能(新建、打开、保存)和编辑功能(撤销、重做)。

效果图:

相关推荐
这里有鱼汤1 分钟前
“对象”?对象你个头!——Python世界观彻底崩塌的一天
后端·python
尘浮72810 分钟前
60天python训练计划----day59
开发语言·python
wh393314 分钟前
使用Python将PDF转换成word、PPT
python·pdf·word
船长@Quant31 分钟前
数学视频动画引擎Python库 -- Manim Voiceover 语音服务 Speech Services
python·数学·manim·动画引擎·语音旁白
Chef_Chen39 分钟前
从0开始学习R语言--Day39--Spearman 秩相关
开发语言·学习·r语言
不学会Ⅳ1 小时前
Mac M芯片搭建jdk源码环境(jdk24)
java·开发语言·macos
好开心啊没烦恼2 小时前
Python 数据分析:计算,分组统计1,df.groupby()。听故事学知识点怎么这么容易?
开发语言·python·数据挖掘·数据分析·pandas
lljss20203 小时前
Python11中创建虚拟环境、安装 TensorFlow
开发语言·python·tensorflow
空中湖3 小时前
tensorflow武林志第二卷第九章:玄功九转
人工智能·python·tensorflow
CodeCraft Studio4 小时前
CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
python·photoshop·cad·aspose·aspose.cad