Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 处理“另存为”和“保存”按钮的功能不工作_Python_File_Tkinter_File Handling - Fatal编程技术网

Python 处理“另存为”和“保存”按钮的功能不工作

Python 处理“另存为”和“保存”按钮的功能不工作,python,file,tkinter,file-handling,Python,File,Tkinter,File Handling,我正在尝试构建记事本,但我的“保存”和“另存为”按钮有问题,因为文本字段中的内容不保存,文本文档总是返回空,以下是我的代码: def saveFile(): global infile global in_path global txt try: if in_path == '': #save as new file in_path = asksaveasfile(initialfile='Untitled.txt'

我正在尝试构建记事本,但我的“保存”和“另存为”按钮有问题,因为文本字段中的内容不保存,文本文档总是返回空,以下是我的代码:

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', 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:
                infile.write(txt.get(1.0, END))
                #change the window title
            root.title(os.path.basename(in_path))
    except:
            pass
def saveFile(): 全局in_路径 全局文本

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(): 全局in_路径 全局文本 尝试: #另存为新文件 在_path=asksaveasfilename(initialfile='Untitled.txt',defaultextension=''.txt',filetypes=[(“所有文件”、“),(“文本文档”、“*.txt”)]中) 如果在路径中==“”: in_path=无 其他: #尝试保存文件 以开放式(in_路径,“w”)作为填充: infle.write(txt.get(1.0,END)) #更改窗口标题 root.title(os.path.basename(in_path)) 除:
pass

我推荐这篇非常有用的文章:。使用
print()
查看执行的代码部分以及变量中的值。这被称为“打印调试”。或者学习如何使用real Debugger。当您在控制台/终端中运行它时,是否会收到错误消息
asksaveasfile
不提供文件名,但提供打开文件的处理程序。要获取文件名,您需要
asksaveasfilename
而不是
asksaveasfile
。使用
打印(在路径中)
查看您得到了什么。