python做一个记事本_python如何自制一个简易记事本?

商绍元
2023-12-01

如何使用Tkinter在Python中创建一个简单的记事本呢。这个记事本GUI将包含各种菜单,如文件和编辑,使用它们可以完成保存文件,打开文件,编辑,剪切和粘贴等所有功能。使用此代码,我们将在记事本的窗口中添加菜单,并将添加复制,粘贴,保存等内容。

def __quitApplication(self):

self.__root.destroy()

# exit()

def __showAbout(self):

showinfo("Notepad", "Mrinal Verma")

def __openFile(self):

self.__file = askopenfilename(defaultextension=".txt",

filetypes=[("All Files","*.*"),

("Text Documents","*.txt")])

if self.__file == "":

# no file to open

self.__file = None

else:

# try to open the file

# set the window title

self.__root.title(os.path.basename(self.__file) + " - Notepad")

self.__thisTextArea.delete(1.0,END)

file = open(self.__file,"r")

self.__thisTextArea.insert(1.0,file.read())

file.close()

def __newFile(self):

self.__root.title("Untitled - Notepad")

self.__file = None

self.__thisTextArea.delete(1.0,END)

def __saveFile(self):

if self.__file == None:

#save as new file

self.__file = asksaveasfilename(initialfile='Untitled.txt',

defaultextension=".txt",

filetypes=[("All Files","*.*"),

("Text Documents","*.txt")])

if self.__file == "":

self.__file = None

else:

# try to save the file

file = open(self.__file,"w")

file.write(self.__thisTextArea.get(1.0,END))

file.close()

# change the window title

self.__root.title(os.path.basename(self.__file) + " - Notepad")

else:

file = open(self.__file,"w")

file.write(self.__thisTextArea.get(1.0,END))

file.close()

def __cut(self):

self.__thisTextArea.event_generate("<>")

def __copy(self):

self.__thisTextArea.event_generate("<>")

def __paste(self):

self.__thisTextArea.event_generate("<>")

 类似资料: