导入模块
import tkinter
import tkinter.messagebox
import tkinter.filedialog
- tkinter - 主要的GUI
- tkinter.messagebox - 用于弹出错误
- 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)
- title - 标题
- geometry - 窗口大小
- resizable - 阻止用户改变窗口大小
主要函数 - 顶部菜单
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)
- menubar - 最顶级菜单
- copyright_menu - menubar上的版权子菜单
- file_menu - 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)
- title - 弹窗的标题
- filetypes - 选择的文件类型
文件 - 保存函数
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()