当前位置: 首页 > article >正文

简单的Python记事本应用程序

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

首先,确保你已经安装了Python和Tkinter库(Tkinter通常是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文件中并运行它。这将启动一个简单的记事本应用程序,带有基本的文件操作功能(新建、打开、保存)和编辑功能(撤销、重做)。

效果图:
在这里插入图片描述


http://www.kler.cn/a/538766.html

相关文章:

  • 第1章 特征工程
  • 基于SpringBoot的线上历史馆藏管理系统
  • Java | RESTful 接口规范
  • 【C/C++】每日温度 [ 栈的应用 ] 蓝桥杯/ACM备赛
  • C语言【基础篇】之数组——解锁多维与动态数组的编程奥秘
  • mysql8 从C++源码角度看sql生成抽象语法树
  • 初次体验Tauri和Sycamore (2)
  • 使用 Apache Spark 进行大数据分析
  • c/c++蓝桥杯经典编程题100道(17)二叉树遍历
  • 网络安全 | F5 BIG-IP RESTful API 模块功能介绍
  • 如何精确掌控网页布局?深入解析 CSS 样式与盒模型
  • 程序员也可以这样赚钱
  • 【R语言】卡方检验
  • 微服务篇-深入了解索引库与文档 CRUD 操作、使用 RestCliet API 操作索引库与文档 CRUD(Java 客户端连接 Elasticsearch 服务端)
  • 递增三元组(蓝桥杯18F)
  • 如何在WPS和Word/Excel中直接使用DeepSeek功能
  • 网络通信的基石:深入理解 TCP/IP 协议栈与 TCP/UDP 协议
  • 在 Windows 上使用 ZIP 包安装 MySQL 的详细步骤
  • react高级面试题
  • Windows Docker笔记-制作、加载镜像
  • 前后端服务配置
  • 从运输到植保:DeepSeek大模型探索无人机智能作业技术详解
  • 【sqlite】python操作sqlite3(含测试)
  • Android 开发APP中参数配置与读取总结
  • Java语言的安全开发
  • DeepSeek 与 Transformer 架构的深度关联