编程语言
首页 > 编程语言> > 0002 - Python记事本

0002 - Python记事本

作者:互联网

文章目录

导入模块

import tkinter
import tkinter.messagebox
import tkinter.filedialog

定义类

class notepad():
    def __init__(self):
        self.main_function()

__init__里的代码用于调用下一个函数

主要函数 - 窗口

	def main_function(self):
    	self.main_window = tkinter.Tk()
        
        self.main_window.title("记事本")
        self.main_window.geometry("400x400")
        self.main_window.resizable(0, 0)

主要函数 - 顶部菜单

		self.menubar = tkinter.Menu(self.main_window)

        self.copyright_menu = tkinter.Menu(self.menubar, tearoff = False)
        self.copyright_menu.add_command(label = "版权", command = lambda: self.copyright_function())
        self.menubar.add_cascade(label = "版权", menu = self.copyright_menu)

        self.file_menu = tkinter.Menu(self.menubar, tearoff = False)
        self.file_menu.add_command(label = "打开", command = lambda: self.open())
        self.file_menu.add_command(label = "保存", command = lambda: self.save())
        self.menubar.add_cascade(label = "文件", menu = self.file_menu)

        self.main_window.config(menu = self.menubar)

主要函数 - 存放输入的文本框

		self.text = tkinter.Text(self.main_window, height = 400, width = 400, font = ("微软雅黑", 10))
        self.text.pack()

主要函数 - 消息循环

		self.main_window.mainloop()

版权函数

    def copyright_function(self):
        tkinter.messagebox.showinfo(title = "版权", message = "作者:黄子恒\ncopyright (c) 保留所有权利")

文件 - 打开函数

    def open(self):
        self.open_name = tkinter.filedialog.askopenfilename(title = "打开记事本", filetypes = [("文本文档", "*.txt"), ("所有文件", "*.*")])
        with open(self.open_name, "w") as file:
            self.open_text = file.read()
        file.close()

        self.text.delete("0.0", "end")
        self.text.insert("0.0", open_text)

文件 - 保存函数

def save(self):
        self.save_name = tkinter.filedialog.asksaveasfilename(title = "保存记事本", filetypes = [("文本文档", "*.txt"), ("所有文件", "*.*")])

        with open(self.save_name, "w") as file:
            file.write(self.text.get("0.0", "end"))
        file.close()

        self.text.delete("0.0", "end")

运行程序

notepad = notepad()

源代码

import tkinter
import tkinter.messagebox
import tkinter.filedialog

class notepad():
    def __init__(self):
        self.main_function()

    def main_function(self):
        self.main_window = tkinter.Tk()
        
        self.main_window.title("记事本")
        self.main_window.geometry("400x400")
        self.main_window.resizable(0, 0)
        
        self.menubar = tkinter.Menu(self.main_window)

        self.copyright_menu = tkinter.Menu(self.menubar, tearoff = False)
        self.copyright_menu.add_command(label = "版权", command = lambda: self.copyright_function())
        self.menubar.add_cascade(label = "版权", menu = self.copyright_menu)

        self.file_menu = tkinter.Menu(self.menubar, tearoff = False)
        self.file_menu.add_command(label = "打开", command = lambda: self.open())
        self.file_menu.add_command(label = "保存", command = lambda: self.save())
        self.menubar.add_cascade(label = "文件", menu = self.file_menu)

        self.main_window.config(menu = self.menubar)

        self.text = tkinter.Text(self.main_window, height = 400, width = 400, font = ("微软雅黑", 10))
        self.text.pack()
        
        self.main_window.mainloop()
        
    def copyright_function(self):
        tkinter.messagebox.showinfo(title = "版权", message = "作者:黄子恒\ncopyright (c) 保留所有权利")

    def open(self):
        self.open_name = tkinter.filedialog.askopenfilename(title = "打开记事本", filetypes = [("文本文档", "*.txt"), ("所有文件", "*.*")])
        
        with open(self.open_name, "w") as file:
            self.open_text = file.read()
        file.close()

        self.text.delete("0.0", "end")
        self.text.insert("0.0", open_text)
        
    def save(self):
        self.save_name = tkinter.filedialog.asksaveasfilename(title = "保存记事本", filetypes = [("文本文档", "*.txt"), ("所有文件", "*.*")])

        with open(self.save_name, "w") as file:
            file.write(self.text.get("0.0", "end"))
        file.close()

        self.text.delete("0.0", "end")
        
notepad = notepad()

标签:tkinter,0002,Python,menu,self,file,text,main,记事本
来源: https://blog.csdn.net/py233333/article/details/112338242