tkinter提醒弹窗自动关闭

不带对话框的提醒功能

import tkinter as tk
class ReminderDialog():
    def __init__(self, title, message):
        self.root = tk.Tk()
        self.root.title(title)
        self.root.geometry("600x100+700+100")
        self.message = message
        self.label = tk.Label(self.root, text=self.message, font=('Arial', 14), justify='center')
        self.label.pack(pady=20)
        self.root.after(5000, self.close)
        self.root.mainloop()
    def close(self):
        self.root.destroy()
# 调用ReminderDialog类创建对话框
ReminderDialog("提醒", "五秒后自动关闭")

带确定对话框的提醒

#带确定按钮的
import tkinter as tk
from tkinter import messagebox
class ReminderDialog():
    def __init__(self, title, message):
        self.root = tk.Tk()
        self.root.title(title)
        self.root.geometry("300x120+700+100")
        self.message = message
        self.label = tk.Label(self.root, text=self.message, font=('微软雅黑', 12), justify='center')
        self.label.pack(pady=20)
        self.root.after(5000, self.close)
        self.ok_button = tk.Button(self.root, text="确定", width=10, command=self.close)
        self.ok_button.pack(pady=5)
        self.root.mainloop()
    def close(self):
        self.root.destroy()
# 调用ReminderDialog类创建对话框
ReminderDialog("运行提醒", "五秒后自动关闭")

win32弹窗自动关闭

import ctypes
import threading
import time

import win32con

user32 = ctypes.windll.user32


def create_dialog(message: str, title: str, style: int = win32con.MB_OK,
                  block: bool = None, interval: float = 0, callback=None):
    """
    使用微软未公布的Windows API: MessageBoxTimeout 实现自动关闭的对话框,通过user32.dll调用,
    相比于使用 MessageBox 来实现显得更加简洁,参数详情请参考以上函数 create_dialog

    值得注意的是 Windows 2000 没有导出该函数。并且对于多选一没有关闭/取消功能的对话框,
    自动关闭时默认(回调)返回值为 32000
    """

    block = block if (block is not None) else interval <= 0
    interval = int(interval * 1000) if interval > 0 else 0

    def show():
        # if UNICODE MessageBoxTimeoutW
        # else MessageBoxTimeoutA
        # MessageBoxTimeout(hwnd, lpText, lpCaption, uType, wLanguageId, dwMilliseconds)
        btn_val = user32.MessageBoxTimeoutW(
            0, message, title, style, 0, interval)
        if callback and callable(callback):
            callback(btn_val)
        return btn_val

    if block:
        return show()
    else:
        threading.Thread(target=show).start()


# 同步阻塞+自动关闭
res = create_dialog('会员情况简介通知流程运行完成', '运行提醒', block=True, interval=5)
print(res)


  目录