Python 保存功能在Tkinter窗口中不工作
我有一个Tkinter程序,它有保存和另存为功能。我把Save设置为工作正常,但Save不是 但是,每当我按下“保存”按钮时,它都会抛出错误: 名称错误:未定义名称“另存为”对话框Python 保存功能在Tkinter窗口中不工作,python,python-3.x,windows,tkinter,windows-10,Python,Python 3.x,Windows,Tkinter,Windows 10,我有一个Tkinter程序,它有保存和另存为功能。我把Save设置为工作正常,但Save不是 但是,每当我按下“保存”按钮时,它都会抛出错误: 名称错误:未定义名称“另存为”对话框 def save_as(): notepad_save = notepad.get('1.0', END).strip('\n') head_save = str(head_entry.get().strip('\n')) + '\n' body_save = str(body_entr
def save_as():
notepad_save = notepad.get('1.0', END).strip('\n')
head_save = str(head_entry.get().strip('\n')) + '\n'
body_save = str(body_entry.get().strip('\n')) + '\n'
tail_save = str(tail_entry.get().strip('\n')) + '\n'
legs_save = str(legs_entry.get().strip('\n')) + '\n'
save_as_dialog = tkfd.asksaveasfilename(initialfile = file_name,
initialdir = '/Users/Documents', title = "Save as", defaultextension =
'.txt', filetypes = [('Text files', '*.txt')])
with open(save_as_dialog, 'w') as output_file:
output_file.write(head_save)
output_file.write(body_save)
output_file.write(tail_save)
output_file.write(legs_save)
output_file.write(notepad_save)
def save():
notepad_save = notepad.get('1.0', END).strip('\n')
head_save = str(head_entry.get().strip('\n')) + '\n'
body_save = str(body_entry.get().strip('\n')) + '\n'
tail_save = str(tail_entry.get().strip('\n')) + '\n'
legs_save = str(legs_entry.get().strip('\n')) + '\n'
try:
with open(save_as_dialog, 'w') as output_file:
output_file.write(head_save)
output_file.write(body_save)
output_file.write(tail_save)
output_file.write(legs_save)
output_file.write(notepad_save)
except NameErrors:
save_as()
如您所见,该文件在不同的行中存储不同的信息。这是程序的一个关键部分,所以我需要保持这部分完整。问题是,当我按下保存按钮时,它会抛出错误:
名称错误:未定义名称“另存为”对话框。
我试着单独使用'except'并使用AttributeError,但它仍然会出现同样的错误。
我卡住了
编辑:我的新代码,仍不工作:
global save_as_dialog
global opened_file_filename
global filepath
filepath = r"C:\Users\Documents"
opened_file_filename = None
def _open():
opened_file_filename = tkfd.askopenfilename(title = "Open...",
defaultextension = '.txt', filetypes = [('Text files', '*.txt')])
def save_as():
notepad_save = notepad.get('1.0', END).strip('\n') #
head_save = head_entry.get().strip('\n') + '\n'
body_save = body_entry.get().strip('\n') + '\n'
tail_save = tail_entry.get().strip('\n') + '\n'
legs_save = legs_entry.get().strip('\n') + '\n'
save_as_dialog = tkfd.asksaveasfilename(initialfile = file_name, initialdir
= filepath, title = "Save as", defaultextension = '.txt', filetypes =
[('Text files', '*.txt')])
with open(opened_file_filename, 'w') as output_file:
output_file.write(head_save)
output_file.write(body_save)
output_file.write(tail_save)
output_file.write(legs_save)
output_file.write(notepad_save)
opened_file_filename = save_as_dialog
time_saved_label.config(text = "Saved" )
print(opened_file_filename)
def save():
notepad_save = notepad.get('1.0', END).strip('\n')
head_save = head_entry.get().strip('\n') + '\n'
body_save = body_entry.get().strip('\n') + '\n'
tail_save = tail_entry.get().strip('\n') + '\n'
legs_save = legs_entry.get().strip('\n') + '\n'
if opened_file_filename is None:
save_as()
else:
with open(opened_file_filename, 'w') as output_file:
output_file.write(head_save)
output_file.write(body_save)
output_file.write(tail_save)
output_file.write(legs_save)
output_file.write(notepad_save)
time_saved_label.config(text = "Saved" )
print(opened_file_filename)
抛出错误:
以open(open_file_filename,'w')作为输出_文件:
FileNotFoundError:[Errno 2]没有这样的文件或目录:“”这是因为您正在
另存为()的对话框中创建变量。变量未在save()
中定义。在这些函数的作用域之外声明变量应该是最简单的方法
在包含两个函数和
然后加上
if save_as_dialog is None:
return save_as()
在save()
函数的开头
结果应该是这样的:
from tkinter import filedialog
# Filename of our file.
opened_file_filename = None
def save_as():
"""
Opens a file dialog and saves stuff to it.
:return: None
"""
global opened_file_filename # we wanna use the opened_file_filename from global scope
opened_file_filename = filedialog.askopenfilename()
with(open(opened_file_filename), 'w') as file:
# write stuff to file here
pass
def save():
"""
If we have a filename, save stuff to it. Otherwise do save_as().
:return: None
"""
global opened_file_filename # we wanna use the opened_file_filename from global scope
if opened_file_filename is None:
# it is none, which means we have not chosen a filename yet
return save_as()
with(open(opened_file_filename), 'w') as file:
# write stuff to file here
pass
# rest of your code
这是因为您正在另存为()
的内部创建另存为对话框
变量。变量未在save()
中定义。在这些函数的作用域之外声明变量应该是最简单的方法
在包含两个函数和
然后加上
if save_as_dialog is None:
return save_as()
在save()
函数的开头
结果应该是这样的:
from tkinter import filedialog
# Filename of our file.
opened_file_filename = None
def save_as():
"""
Opens a file dialog and saves stuff to it.
:return: None
"""
global opened_file_filename # we wanna use the opened_file_filename from global scope
opened_file_filename = filedialog.askopenfilename()
with(open(opened_file_filename), 'w') as file:
# write stuff to file here
pass
def save():
"""
If we have a filename, save stuff to it. Otherwise do save_as().
:return: None
"""
global opened_file_filename # we wanna use the opened_file_filename from global scope
if opened_file_filename is None:
# it is none, which means we have not chosen a filename yet
return save_as()
with(open(opened_file_filename), 'w') as file:
# write stuff to file here
pass
# rest of your code
你说的范围是什么意思?我在save()函数中尝试过,但没有成功?对不起,我在过去6个月才学习python,所以我还不知道所有的术语。你介意详细说明一下吗?作用域是一种限制变量存在位置的方法。当您在python脚本的根中声明变量时(没有缩进),它可以在python脚本中随处可见。如果在函数中定义它,则只能在该函数中看到它。那么,你认为我应该把save_as_dialog=None放在哪里?我是否应该创建一个包含save()和save_as()的函数,并将save_as_dialog=None放在那里?如果是这样,我将如何从该函数中调用save_as()和save()?我将把它放在与save_as()
和save()
相同的级别上。很高兴听到这个消息!如果你想聊一聊python,顺便看看python discord。你说的范围是什么意思?我在save()函数中尝试过,但没有成功?对不起,我在过去6个月才学习python,所以我还不知道所有的术语。你介意详细说明一下吗?作用域是一种限制变量存在位置的方法。当您在python脚本的根中声明变量时(没有缩进),它可以在python脚本中随处可见。如果在函数中定义它,则只能在该函数中看到它。那么,你认为我应该把save_as_dialog=None放在哪里?我是否应该创建一个包含save()和save_as()的函数,并将save_as_dialog=None放在那里?如果是这样,我将如何从该函数中调用save_as()和save()?我将把它放在与save_as()
和save()
相同的级别上。很高兴听到这个消息!如果您想聊一聊python,请访问python discord。在save()
function的开头添加global save\u as\u对话框
。首先从所有get()
方法中删除str()
get()
已返回字符串。这里的问题是,save\u as\u对话框
是在save\u as()
函数中定义的,但是您尝试从save()
函数调用它。您需要将save_as_对话框
传递到save_as()
或使其成为global
变量。在save()
函数的开头添加global save_as_对话框
。首先从所有get()
方法中删除str()
get()
已返回字符串。这里的问题是,save\u as\u对话框
是在save\u as()
函数中定义的,但是您尝试从save()
函数调用它。您需要将save_as_对话框
传递到save_as()
或使其成为全局
变量。