首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python TkMessageBox问题不起作用!

Python TkMessageBox问题不起作用!
EN

Stack Overflow用户
提问于 2011-07-11 23:07:53
回答 2查看 6.1K关注 0票数 1

我有一个将输出写入文件的按钮。并检查具有该文件名的文件是否已经存在。它是否应该要求用户重写?但它不起作用。如果用户选择No,则程序仍将覆盖该文件。

以下是弹出MessageBox的代码:

代码语言:javascript
复制
    if os.path.exists(self.filename.get()):
        tkMessageBox.showinfo('INFO', 'File already exists, want to override?.')
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-11 23:25:13

您需要使用具有yes/no或ok/cancel按钮的对话框,并且需要捕获该对话框的返回值以了解用户单击了什么。然后,您可以决定是否写入该文件。

例如:

代码语言:javascript
复制
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.orgstandard dialogs上写了一篇很好的文章

票数 5
EN

Stack Overflow用户

发布于 2011-07-11 23:13:58

代码语言:javascript
复制
if os.path.exists(self.filename.get()) and tkMessageBox.askyesno(title='INFO', message='File already exists, want to override?.'):
    # Overwrite your file here..
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6652263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档