Notepad project development
""" Developing Notepad software """ # -*- coding: utf-8 -*- from tkinter import * from tkinter.colorchooser import * from tkinter.filedialog import * from tkinter.messagebox import * class Application(Frame): def __init__(self,master=None): super().__init__(master) self.master = master self.textpad = None self.filename = None self.pack() self.createWidget() def createWidget(self): # Create main menu menubar = Menu(root) menuFile = Menu(menubar) menuEdit = Menu(menubar) menuHelp = Menu(menubar) # Add submenu to main menu bar menubar.add_cascade(label="text(F)", menu=menuFile) menubar.add_cascade(label="edit(E)", menu=menuEdit) menubar.add_cascade(label="Help(H)", menu=menuHelp) # Add menu item menuFile.add_command(label="new file",accelerator="ctrl+n",command=self.newfile) menuFile.add_command(label="Open file",accelerator="ctrl+o",command=self.openfile) menuFile.add_command(label="Save file",accelerator="ctrl+s",command=self.savefile) menuFile.add_separator() # Add divider menuFile.add_command(label="Exit file",accelerator="ctrl+q",command=self.quitfile) #Create shortcut menu bar self.menubar2 = Menu(root) self.menubar2.add_command(label="background color",command=self.openAskColor) menuedit = Menu(self.menubar2, tearoff=0) # If the tearoff is 1, the menu can be independent. If the tearoff is 0, the menu cannot be independent. menuedit.add_command(label="shear") menuedit.add_command(label="copy") menuedit.add_command(label="paste") self.menubar2.add_cascade(label="edit", menu=menuedit) # Add the main menu bar to the root window root["menu"] = menubar #text editing region self.textpad = Text(root,width=80,height=50) self.textpad.pack() w1 = Text(root, width=50, height=30) w1.pack() root.bind("<Button-3>", self.test1) 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.quit()) def test1(self,event): # The menu appears at the right-click coordinates self.menubar2.post(event.x_root,event.y_root) def newfile(self): c1 = self.textpad.get(1.0, END) if not c1.isspace(): a1=askquestion(title="Tips",message="Do you want to save this file") if a1 == "yes": self.savefile() self.textpad.delete(1.0, END) def openfile(self): self.textpad.delete("1.0",END) with askopenfile(title="Select a text file to open") as f: self.textpad.insert(INSERT, f.read()) self.filename = f.name print(f.name) def savefile(self): c1 = self.textpad.get(1.0, END) c1.isspace() self.filename = asksaveasfilename(title="Save as", initialfile="Unnamed.txt", filetypes=[("Text document", "*.txt")], defaultextension='.txt') with open(self.filename, "w") as f: f.write(c1) def quitfile(self): a1 = askquestion(title="Tips", message="Do you want to save this file") if a1 == "yes": self.savefile() root.quit() def openAskColor(self): s1 = askcolor(color="red", title="Choose a background color") self.textpad.config(bg=s1[1]) if __name__ == '__main__': root = Tk();root.geometry("450x300+300+200") root.title("Mr_huang Simple Notepad for") app = Application(master=root) root.mainloop()
[PROJECT] package python program into exe file
We can use the pyinstaller module to package python projects into exe files. The operation steps are as follows:
- Installing the pyinstaller module
Operate in pycharm: file - > setting - > Project: XXX - > project interpreter, and then click
Just hit +. - Enter the following command at the Terminal of pycharm:
pyinstaller -F xxxx.py
[note] relevant parameters are as follows:
– icon = icon path (pyinstaller -F --icon=my.ico XXXX.py)
-F is packaged as an exe file
-w use window, no console
-c use the console without windows
-D create a directory containing exe and other dependent files - In the dist directory of the project, you can see that the generated exe file can be used directly in the windows system.
[note] the essence of exe file is to package python interpreter and program together, so that we do not need to execute the program.
Whether the windows system has a python interpreter or not.
[note] Currently, pyinstaller only supports Python 3.6. If you are Python 3.7, you may fail.