Итак, я создаю приложение Notepad, используя Python 3x и Tkinter. Каждая функция работает правильно, но «Сохранить» не так, и я знаю почему. Вот код:
textArea = Text(root)
menuBar = Menu(root)
fileMenu = Menu(menuBar, tearoff=0)
editMenu = Menu(menuBar, tearoff=0)
helpMenu = Menu(menuBar, tearoff=0)
scrollBar = Scrollbar(textArea)
file = None
def init():
textArea.grid(sticky=N + E + S + W)
fileMenu.add_command(label="New", command=newFile)
fileMenu.add_command(label="Open", command=openFile)
fileMenu.add_command(label="Save", command=saveFile)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=exitApp)
menuBar.add_cascade(label="File", menu=fileMenu)
root.config(menu=menuBar)
scrollBar.pack(side=RIGHT, fill=Y)
scrollBar.config(command=textArea.yview)
textArea.config(yscrollcommand=scrollBar.set)
def newFile():
root.title('Untitled - Notepad')
file = None
textArea.delete(1.0, END)
def openFile():
file = tkinter.filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file == '':
file = None
else:
root.title(os.path.basename(file) + " - Notepad")
textArea.delete(1.0, END)
file = open(file, "r")
textArea.insert(1.0, file.read())
file.close()
def saveFile():
if file == None:
file = tkinter.filedialog.asksaveasfilename(defaultextension=".txt",
initialfilename="Untitled.txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file == '':
file = None
else:
file = open(file, "w")
file.write(textArea.get(1.0, END))
file.close()
root.title(os.path.basename(file) + " - Notepad")
else:
file = open(file, "w")
file.write(textArea.get(1.0, END))
file.close()
def exitApp():
exit()
init()
root.mainloop()
Отладчик говорит, что ошибка находится в строке 66, когда я использую «файл» в качестве ссылки, но хочу найти другой способ сделать функцию сохранения.
if file == None: