我有一个将输出写入文件的按钮。并检查具有该文件名的文件是否已经存在。它是否应该要求用户重写?但它不起作用。如果用户选择No,则程序仍将覆盖该文件。
以下是弹出MessageBox的代码:
if os.path.exists(self.filename.get()):
tkMessageBox.showinfo('INFO', 'File already exists, want to override?.')发布于 2011-07-11 23:25:13
您需要使用具有yes/no或ok/cancel按钮的对话框,并且需要捕获该对话框的返回值以了解用户单击了什么。然后,您可以决定是否写入该文件。
例如:
import Tkinter as tk
import tkMessageBox
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.button = tk.Button(self, text="Push me", command=self.OnButton)
self.button.pack()
def OnButton(self):
result = tkMessageBox.askokcancel(title="File already exists",
message="File already exists. Overwrite?")
if result is True:
print "User clicked Ok"
else:
print "User clicked Cancel"
if __name__ == "__main__":
app = SampleApp()
app.mainloop()effbot.org在standard dialogs上写了一篇很好的文章
发布于 2011-07-11 23:13:58
if os.path.exists(self.filename.get()) and tkMessageBox.askyesno(title='INFO', message='File already exists, want to override?.'):
# Overwrite your file here..https://stackoverflow.com/questions/6652263
复制相似问题