tkinter -button在处理完整功能(与按钮关联)之前不显示标签

时间:2017-09-11 07:09:45

标签: python tkinter

我正在使用tkinter中的python 2.7创建一个gui。在GUI中有按钮执行功能。问题是,单击该按钮时,在整个功能完成之前,它不会显示标签。示例代码:

button4=tk.Button(self,text="MEMORY",height=1,width=20,font=LABEL_FONT,fg=mycolor,bg='light blue',command=lambda: controller.show_frame(memory)) 
button4.place(relx=0.6, rely=.53,width=120, anchor="sw")

与按钮相关的功能是:

def memory(self):
    label = tk.Label(
        self,
        text="Calculating, Please wait...",
        font=text_font,
        fg="white",
        bg=mycolor)
    label.place(relx=.0005, rely=.17)
    f3 = open('filename' ,"r")
    list1= list()
    list2= list()

    # Calculating some values which takes around 5 minutes as the lists are huge

此处文本“Please wait ...”仅在5分钟后显示,即。计算后。我需要在单击按钮后立即显示文本(标签)。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在开始长时间计算之前调用更新

def memory(self):
    label = tk.Label(
        self,
        text="Calculating, Please wait...",
        font=text_font,
        fg="white",
        bg=mycolor)
    label.place(relx=.0005, rely=.17)
    self.update()
    f3 = open('filename' ,"r")
    list1= list()
    list2= list()

    # Calculating some values which takes around 5 minutes as the lists are huge

但你最好使用线程