建立程序
from tkinter import *from tkinter.colorchooser import *from tkinter.filedialog import *class Application(Frame):def __init__(self, master=None):super().__init__(master)self.master = master #super()代表的是父类的定义,而不是父类对象self.textpad = None #textpad表示Text文本框对象self.pack()self.createWiget()def createWiget(self):#创建主菜单栏menubar = Menu(root)#创建字菜单menFile = Menu(menubar)menEdit = Menu(menubar)menHelp = Menu(menubar)#将子菜单加入到主菜单栏menubar.add_cascade(label="文件(F)",menu=menFile)menubar.add_cascade(label="编辑(E)", menu=menEdit)menubar.add_cascade(label="帮助(H)", menu=menHelp)#添加菜单项menFile.add_cascade(label="新建",accelerator="ctrl+n",command=self.newfile)menFile.add_cascade(label="打开", accelerator="ctrl+o",command=self.openfile)menFile.add_cascade(label="保存", accelerator="ctrl+s",command=self.savefile)menFile.add_separator() #添加分割线menFile.add_cascade(label="退出", accelerator="ctrl+q",command=self.exit)#将主菜单栏加到根窗口root["menu"]=menubar#增加快捷键的处理root.bind("<Control-n>",lambda event:self.newfile())root.bind("<Control-o>",lambda event:self.openfile())root.bind("<Control-s>", lambda event: self.savefile())root.bind("<Control-q>", lambda event: self.exit())# 文本编辑区self.textpad = Text(root, width=50, height=30)self.textpad.pack()#创建上下菜单self.contextMenu = Menu(root)self.contextMenu.add_command(label="背景颜色",command=self.openAskColor)#为右键绑定事件root.bind("<Button-3>",self.createContextMenu)def newfile(self):self.textpad.delete(1.0, END) # 把text控件中所有内容清空self.filename = asksaveasfilename(title="另存为",initialfile="未命名.txt",filetypes=[("文本文档","*.txt")],defaultextension=".txt")self.savefile()def openfile(self):self.textpad.delete(1.0,END)#把text控件中所有内容清空with askopenfile(title="打开文本文件") as f:self.textpad.insert(INSERT,f.read())self.filename = f.namedef savefile(self):with open(self.filename,"w") as f:c = self.textpad.get(1.0,END)f.write(c)def exit(self):root.quit()def openAskColor(self):s1 = askcolor(color="red",title="返回背景色")self.textpad.config(bg=s1[1])def createContextMenu(self,event):#菜单在鼠标右键单击的坐标处显示self.contextMenu.post(event.x_root,event.y_root)if __name__ == "__main__":root = Tk()root.geometry("450x300+600+200")root.title("简易记事本")app = Application(master=root)root.mainloop()

打包程序
①打开file ——>setting

②选择Project:xxx.py——> Python Interprete

③点击旁边的“+”号

④在搜索框中搜索:pyinstaller ——> 并点击下方Install Package
⑤等待安装成功
⑥打开控制台


⑦打包——> 控制台输入pyinstaller -F xxx.py

【注】:相关参数如下:
- —icon=图标路径(pyinstall -F —icon=my.icon xxx.py)
- -F 打包成一个exe文件
- -W 使用窗口,无控制台
- -C 使用控制台,无窗口
- -D 创建一个目录,里面包含exe以及其他一些依赖性文件
⑧打开项目下的dist目录



