我正在尝试构建一个记事本,但是我的save和saveas按钮出现问题,因为TextField中的内容没有保存,文本文档始终返回空,这是我的代码:
def saveFile():
global infile
global in_path
global txt
try:
if in_path == '':
#save as new file
in_path = asksaveasfile(initialfile='Untitled.txt',defaultextension='txt',filetypes=[("All Files","*.*"),("Text Documents","*txt")])
if in_path == "":
in_path = None
else:
#try to save file
with open(in_path,"w") as infile:
data = (txt.get(1.0,END))
infile.write(data)
#change the window title
root.title(os.path.basename(in_path))
else:
#try to save file
with open(in_path,"w") as infile:
infile.write(txt.get(1.0,END))
#change the window title
root.title(os.path.basename(in_path))
except:
pass
def saveAs():
global infile
global in_path
global txt
try:
#save as new file
in_path = asksaveasfile(initialfile='Untitled.txt',"*txt")])
if in_path == " ":
in_path = None
else:
#try to save file
with open(in_path,END))
#change the window title
root.title(os.path.basename(in_path))
except:
pass