savefile函数在我的记事本gui上不起作用显示名称错误

tjvv9vkg  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(109)

下面是我的记事本gui代码,一切正常,但我不知道为什么 saveFile() 不行!

file = None

def saveFile(self=self):
        global file
        if file == None:
            file = asksaveasfilename(initialfile="Untitled.txt", defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])

            if file == "":
                file = None
            else:
                f = open(file, 'w')
                f.write(self.WritingArea.get(1.0, tk.END))
                f.close()
                self.title(os.path.basename(file) + "- Notepad")

        else:
            f = open(file, 'w')
            f.write(self.WritingArea.get(1.0, tk.END))
            f.close()

我不知道为什么它不起作用,但它表明:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "f:\ERIQ\PYTHON\Notepad\Notepad.py", line 35, in saveFile
    if file == None:
NameError: name 'file' is not defined

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题