【问题标题】:MessageBox pause - Python消息框暂停 - Python
【发布时间】:2020-09-02 14:06:39
【问题描述】:

我正在编写 python 脚本,其中我有带有删除帐户按钮的用户 GUI。用户按下删除帐户按钮后,会出现一个弹出消息框(我使用 tkinter 消息框,但其他选项也不错),要求他批准该任务。我想添加一个选项来暂停几秒钟,用户必须等待几秒钟才能点击确定。 如果您知道实现这种可能性的方法(不一定是 tkinter),我真的很想知道它。 感谢所有帮助者。

【问题讨论】:

  • 暂停是在消息框弹出之后,点击之前。:你必须使用你自己的Dialog WindowButton@ 987654322@ 和 to use after method 启用延迟。
  • 我不认为 tkinter 有这个功能。可能是唯一的方法是@stovfl 上面已经解释过。
  • 嗨小丑,如果答案解决了你的问题,你可以accept它。如果您有任何疑问,请随时告诉我。

标签: python winapi tkinter messagebox confirmation


【解决方案1】:

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

我是这样做的。

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) 或 您可以将其保留在主文件的开头

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

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".

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 2015-06-12
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2018-06-10
    • 1970-01-01
    相关资源
    最近更新 更多