Tkinter标签不更新

2 投票
1 回答
1579 浏览
提问于 2025-04-18 09:27

有没有人能解释一下,为什么这个代码不会更新 label3 呢?

self.label3Text = StringVar()
self.label3Text.set("0/0")
self.label3 = Label(textvariable=self.label3Text)
self.label3.pack()

for root,dirs,filenames in os.walk(self.path):
    #iterate through the files
    num_files = len(filenames)
    i = 0
    for f in filenames:
        #if the file is a text file
        if ".txt" in f:
            #define the path of this new file as the datalog folder plus the filename
            file_path = os.path.join(self.path,f)
            #write the correct data to the csv file for this file
            self.addRows(f,file_path)
            i = i + 1
            out = str(i) + "/" + str(num_files) + " files processed"
            self.label3Text.set(out)

1 个回答

2

好的,答案是我需要更新我的主窗口!通过调用 window.update(),我解决了这个问题。而 window 是这样声明的:window = Tk()

我把这个加上来,以防其他人遇到这个问题时能看到。

撰写回答