为什么在tkinter中点击按钮调用Toplevel窗口时需要等数据处理完后才弹出窗口

为什么在tkinter中点击按钮调用Toplevel窗口时需要等数据处理完后才弹出窗口,而不是点击按钮后一直显示Toplevel窗口观看其标签的变动?

import time
from collections import OrderedDict
import tkinter as tk

def t_tuple(win):
    top = TopLevel(win, 300, 200)
    group = []
    top.label1['text'] = '1、正在创建组合。。。。。。'
    star = time.time()
    for i in range(36-5):
        for j in range(i+1, 36-4):
            for k in range(j+1, 36-3):
                for l in range(k+1, 36-2):
                    for n in range(l+1, 36-1):
                        for m in range(n+1, 36):
                            result_int = [i, j, k, l, n, m]
                            result_int.sort()

                            str1 = '-'.join(map(str, result_int))
                            group.append(str1)
    end = time.time()
    top.label1['text'] = '1、组合创建完成,用时:%.2f' % (end-star)
    top.label2['text'] = '2、开始过滤数据。。。。。。'
    star2 = time.time()
    my_list = list(OrderedDict.fromkeys(group))
    end2 = time.time()
    top.label2['text'] = '2、数据过滤完成,用时:%.2f' % (end2-star2)

def center(root , width, height):
    screen_width, screen_height = root.winfo_screenwidth(), root.winfo_screenheight()
    root.geometry(f'{width}x{height}+{(screen_width-width)//2}+{(screen_height-height)//2}')

class TopLevel(tk.Toplevel):
    def __init__(self, root, width, height):
        super().__init__(root)
        center(self, width, height)
        self.wm_overrideredirect(False)
        frame = tk.Frame(self, bg='green')
        frame.pack(side=tk.LEFT, fill='both', expand=1)
        self.label1 = tk.Label(frame, font=('微软雅黑', 10), justify=tk.LEFT, bg='green')
        self.label1.grid(row=0, column=0, padx=10, pady=1, sticky='w')
        self.label2 = tk.Label(frame, font=('微软雅黑', 10), justify=tk.LEFT, bg='green')
        self.label2.grid(row=1, column=0, padx=10, pady=1, sticky='w')
        self.buttom1 = tk.Button(frame, text='关闭', font=('微软雅黑', 10), command=self.destroy, anchor='center',
  width=10)
    self.buttom1.place(relx=0.35, rely=0.75)

    self.transient()  # 定义为临时窗口
    self.grab_set()
    self.wm_attributes("-topmost", 1)  # 将窗口置顶

if __name__ == '__main__':
    root = tk.Tk()
    root.title('测试')
    center(root, 500, 200)
    font = ('宋体', 15)
    tk.Button(root, text='启动', font=font, command=lambda:     t_tuple(root)).pack()

    root.mainloop()
Jason990420
最佳答案

The GUI won't update immediately after you update the tkinter architecture, or it will update when idle after other tasks done.

Try to add following statement when you need to to update immediately.

    # After statement >> top.label1['text'] = '1、正在创建组合。。。。。。'
    top.update()    # or root.update()
1个月前 评论
讨论数量: 1
Jason990420

The GUI won't update immediately after you update the tkinter architecture, or it will update when idle after other tasks done.

Try to add following statement when you need to to update immediately.

    # After statement >> top.label1['text'] = '1、正在创建组合。。。。。。'
    top.update()    # or root.update()
1个月前 评论

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