Don*_*ode 1 python tkinter tkmessagebox
我有一个将输出写入文件的按钮.并检查具有该文件名的文件是否已存在.它应该要求用户覆盖或不覆盖?但它没有用.如果用户说"否",则程序仍将覆盖该文件.
这是弹出MessageBox的代码:
if os.path.exists(self.filename.get()):
tkMessageBox.showinfo('INFO', 'File already exists, want to override?.')
Run Code Online (Sandbox Code Playgroud)
您需要使用具有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()
Run Code Online (Sandbox Code Playgroud)
effbot.org在标准对话框上有一个很好的小写
| 归档时间: |
|
| 查看次数: |
5992 次 |
| 最近记录: |