首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MessageBox暂停- Python

MessageBox暂停- Python
EN

Stack Overflow用户
提问于 2020-05-16 18:07:43
回答 1查看 1.1K关注 0票数 2

我正在编写python脚本,其中有用户GUI和delete帐户按钮。用户按下delete帐户按钮后,会有一个弹出消息框(我使用,但其他选项也不错),这些选项要求他批准这项任务。我想添加一个选项,使一些秒暂停,用户将必须等待几秒钟,贝多,点击确定。如果你知道实现这种可能性的方法(不一定是平淡的),我真的很想知道。谢谢所有的帮手。

EN

回答 1

Stack Overflow用户

发布于 2020-05-17 00:33:50

通过使用基类对话框tkinter.simpledialog模块,我们可以创建任何自定义对话框。

我就是这么做的。

代码语言:javascript
复制
from tkinter import *
import tkinter.simpledialog as sd


class WaitAlert(sd.Dialog):
    """An alert which will wait for a given time before user can interact.

    Args:
        parent: Takes the parent window instance.
        title (str): Main heading of the alert.
        message (str): Information to display.
        pause (int): Time till inactive. (in seconds)
        show_timer (boolean): Shows countdown."""

    def __init__(self, parent, title=None, message=None, pause=None, show_timer=False):
        self.message = message or ''
        self.pause = pause
        self.show_timer = show_timer
        super().__init__(parent, title=title)

    def body(self, master):
        # For macOS, we can use the below command to achieve a window similar to an alert.
        # Comment the below line if you are on windows.
        self.tk.call("::tk::unsupported::MacWindowStyle", "style", self._w, "moveableAlert")
        Label(master, text=self.message).pack()

    def _timer(self, count, b1, b2):
        "Timer function."
        if count > 0:
            if self.show_timer: b1['text'] = str(count)
            self.after(1000, self._timer, count-1, b1, b2)
        else:
            if self.show_timer: b1['text'] = "OK"
            b1['state'] = 'normal'
            b2['state'] = 'normal'

    def buttonbox(self):
        box = Frame(self)
        b1 = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE, state='disabled')
        b1.pack(side=LEFT, padx=5, pady=5)
        b2 = Button(box, text="Cancel", width=10, command=self.cancel, state='disabled')
        b2.pack(side=LEFT, padx=5, pady=5)
        if self.pause is not None: 
            self._timer(self.pause, b1, b2)
        self.bind("<Return>", self.ok)
        self.bind("<Escape>", self.cancel)
        box.pack()

    def apply(self):
        self.result = True
        return super().apply()

现在,您可以将该类保存在单独的python文件中,并通过导入使用它。例如,我将其保存为tkDialog.py,然后将其导入主文件(from tkDialog import WaitAlert),或者将其保存在主文件的开头。

下面是一个关于如何使用它的小例子。

代码语言:javascript
复制
from tkinter import *
from tkDialog import WaitAlert

root = Tk()
# `wm_withdraw()` will hide the window from the screen.
root.wm_withdraw()
popup = WaitAlert(parent=root,
              title='Alert!', 
              message='Do you want to delete this account?', 
              pause=5,  # pauses for 5 secs.
              show_timer=True) # show countdown.
print(popup.result)   # get the result.
# If the user clicks "OK" the result will return "True" else return "None".

希望这能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61841410

复制
相关文章

相似问题

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