如何设置让tkinter的messagebox消息框动态更新提示信息

如何设置让tkinter的messagebox消息框动态更新提示信息,如:刚开始提示信息为:正在处理数据中。。。。。。,当运行玩数据后提示信息改为:数据处理完成,用时xx秒,等待1秒后关闭消息框。

Jason990420
最佳答案

The message of a tkinter messagebox cannot bu updated.

You have to build one by yourself, like

from time import sleep
from tkinter import *

class Popup(Toplevel):

    def __init__(self, width=480, height=240):
        super().__init__(root)
        self.wm_overrideredirect(True)  # No titlebar
        self["bg"] = "green"
        center(self, width, height)

        note = Label(self, text="This is a popup window ....", fg="white", bg="blue")
        note.pack()

        frame = Frame(self)
        frame.pack(side=LEFT, fill=X, expand=1)

        label = Label(frame, font=font, justify=CENTER, wraplength=width-20, bg='green', fg='white')
        label.pack(fill=BOTH, expand=1)

        self.label = label

        # Set popup to modal mode
        self.transient()
        self.grab_set()
        self.wm_attributes("-topmost", 1)

def job(popup, counter):
    if counter % 10 == 0:
        popup.label["text"] = f"Step {counter//10} of 10"
    label["text"] = f"Counter is {counter}"
    counter += 1
    if counter <= 100:
        root.after(50, lambda:job(popup, counter))
    else:
        root.after(1000, popup.destroy)

def run():
    counter = 0
    popup = Popup()
    root.after(50, lambda:job(popup, counter))

def center(win, win_width, win_height):
    screen_width, screen_height = root.winfo_screenwidth(), root.winfo_screenheight()
    win.geometry(f"{win_width}x{win_height}+{(screen_width-win_width)//2}+{(screen_height-win_height)//2}")

font = ("Courier New", 40)

root = Tk()
root_width, root_height = 640, 480
center(root, root_width, root_height)

button = Button(root, text='START', font=font, command=run)
button.pack(fill=BOTH, expand=1)

label = Label(root, font=font)
label.pack(side=BOTTOM, fill=X)

root.mainloop()

file


It will be much easy if you use one widget on the main window, like a status bar or something else.

1个月前 评论
Jason990420 (作者) 1个月前
LiTtianS (楼主) 1个月前
讨论数量: 6

GPT写的


import tkinter as tk
import time

class DynamicMessageBox:
    def __init__(self, root):
        self.root = root
        self.root.title("Dynamic MessageBox")
        self.message_var = tk.StringVar()
        self.message_var.set("正在处理数据中....")
        self.label = tk.Label(root, textvariable=self.message_var)
        self.label.pack(padx=10, pady=10)
        self.root.after(1000, self.process_data)  # 1秒后开始数据处理

    def process_data(self):
        # 模拟数据处理,这里sleep 5秒钟
        time.sleep(5)

        # 更新消息框文本
        elapsed_time = 5  # 实际数据处理用时
        self.message_var.set(f"数据处理完成,用时 {elapsed_time} 秒,等待 1 秒后关闭消息框.")
        self.root.update_idletasks()  # 立即更新界面

        # 1秒后关闭消息框
        self.root.after(1000, self.close_message_box)

    def close_message_box(self):
        self.root.destroy()

def main():
    root = tk.Tk()
    app = DynamicMessageBox(root)
    root.mainloop()

if __name__ == "__main__":
    main()
1个月前 评论
LiTtianS (楼主) 1个月前

我用的是zhile大佬的。我私发给你了。

1个月前 评论
Jason990420

The message of a tkinter messagebox cannot bu updated.

You have to build one by yourself, like

from time import sleep
from tkinter import *

class Popup(Toplevel):

    def __init__(self, width=480, height=240):
        super().__init__(root)
        self.wm_overrideredirect(True)  # No titlebar
        self["bg"] = "green"
        center(self, width, height)

        note = Label(self, text="This is a popup window ....", fg="white", bg="blue")
        note.pack()

        frame = Frame(self)
        frame.pack(side=LEFT, fill=X, expand=1)

        label = Label(frame, font=font, justify=CENTER, wraplength=width-20, bg='green', fg='white')
        label.pack(fill=BOTH, expand=1)

        self.label = label

        # Set popup to modal mode
        self.transient()
        self.grab_set()
        self.wm_attributes("-topmost", 1)

def job(popup, counter):
    if counter % 10 == 0:
        popup.label["text"] = f"Step {counter//10} of 10"
    label["text"] = f"Counter is {counter}"
    counter += 1
    if counter <= 100:
        root.after(50, lambda:job(popup, counter))
    else:
        root.after(1000, popup.destroy)

def run():
    counter = 0
    popup = Popup()
    root.after(50, lambda:job(popup, counter))

def center(win, win_width, win_height):
    screen_width, screen_height = root.winfo_screenwidth(), root.winfo_screenheight()
    win.geometry(f"{win_width}x{win_height}+{(screen_width-win_width)//2}+{(screen_height-win_height)//2}")

font = ("Courier New", 40)

root = Tk()
root_width, root_height = 640, 480
center(root, root_width, root_height)

button = Button(root, text='START', font=font, command=run)
button.pack(fill=BOTH, expand=1)

label = Label(root, font=font)
label.pack(side=BOTTOM, fill=X)

root.mainloop()

file


It will be much easy if you use one widget on the main window, like a status bar or something else.

1个月前 评论
Jason990420 (作者) 1个月前
LiTtianS (楼主) 1个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!