Tkinter标签文本不会更新/覆盖

时间:2018-10-25 20:17:36

标签: python tkinter

from Tkinter import *

self.btn_run = Button(window, text="Run Model", command=self.clicked_run)
self.btn_run.grid(column=2, row=6)
self.lbl_finish = Label(window, text="")
self.lbl_finish.grid(column=2, row=7)


# Submit button for run model
def clicked_run(self):        
    self.lbl_finish.configure(text="")

    if len(train_SKU) == 0 or len(test_SKU) == 0:
        tkMessageBox.showinfo('ERROR', 'Train or Test is empty!')
    else:
        train_model() # trains the machine learning model
        self.lbl_finish.configure(text="Finished!")

基本上,我有一个“运行模型”按钮,它可以运行我的机器学习模型。完成之后,我想输入一个文本,说“ Finished!”。按钮下方。当再次按下“运行模型”按钮时,我想要“完成!”文本仅在ML模型完成后才清除并显示。但是,现在,在第一个“完成!”之后出现,即使再次按下“运行模型”按钮也不会消失。

1 个答案:

答案 0 :(得分:1)

您可以使用StringVariable并更新窗口

import tkinter as tk
from time import sleep

window = tk.Tk()

var = tk.StringVar()
var.set('')
lbl_finish = tk.Label(window, textvariable=var)
lbl_finish.grid(column=2, row=7)

def clicked_run():
    var.set('')
    window.update()
    sleep(5)
    var.set('Finshed')

btn_run = tk.Button(window, text="Run Model", command=clicked_run)
btn_run.grid(column=2, row=6)

if __name__ == '__main__':
    window.mainloop()