处理另存为和保存按钮的功能不起作用

我正在尝试构建一个记事本,但是我的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
lokeven 回答:处理另存为和保存按钮的功能不起作用

def saveFile():     全局输入路径     全局txt

if in_path == '':
#save as new file
    in_path = asksaveasfilename(initialfile='Untitled.txt',defaultextension='.txt',filetypes=[("All Files","*.*"),("Text Documents","*.txt")])
    if in_path == "":
        in_path = None
    else:
        try:
            #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
else:
        #try to save file
    with open(in_path,"w") as infile:
        infile.write(txt.get(1.0,END))
            #change the window title
            #infile.close()
        root.title(os.path.basename(in_path))

def saveAs():     全局输入路径     全局txt     尝试:     #另存为新文件         in_path = asksaveasfilename(initialfile ='Untitled.txt',defaultextension ='。txt',filetypes = [(“所有文件”,“ ”),(“文本文档”,“ *。txt “)])         如果in_path ==“”:             in_path =无         其他:                 #尝试保存文件             使用open(in_path,“ w”)作为infile:                 infile.write(txt.get(1.0,END))                 #更改窗口标题             root.title(os.path.basename(in_path))     除了:             通过

本文链接:https://www.f2er.com/2983307.html

大家都在问