Tkinter标签在创建时显示和消失

2024-10-02 10:26:30 发布

您现在位置:Python中文网/ 问答频道 /正文

在我创建了一个框架并用root.mainloop启动GUI之后,我正在尝试向它添加新标签(我可能会切换到按钮,但我认为行为是相同的)

代码如下:

import tkinter as tk
import config
import os
from PIL import ImageTk, Image

class JulieGui():
    dirLabels = []
    def __init__(self, queue):
        self.currentDirDisplayed = False
        root = tk.Tk()
        self.root = root
        self.root.geometry("600x400+0+0")
        self.root.title("Julie")
        self.currentDirLabel = tk.Label(self.root, text="Current dir : ", font="Arial 12", width=600)
        self.currentDirLabel.pack()
        queue.put(self)
        self.run()

def updateDir(self):
    self.currentDir.set(self.currentDirPath)

def runLoop(self):
    self.currentDirLabel.configure(text="CurrentDir : {}".format(config.currentDirPath))
    self.printCurrentDir()
    self.root.after(200, self.runLoop)

def printCurrentDir(self):
    if config.currentDirChanged or not self.currentDirDisplayed:
        print("showing dir")
        self.currentDirDisplayed = True
        nbFiles = 0;
        x = 0
        y = 50
        for f in config.currentDirContent:
            if os.path.isdir(f):
                print("file : {}".format(f))
                print("nb labels : {}".format(len(self.dirLabels)))
                frame = tk.Frame(self.root, width= 500, height = 500, bd = 2, relief = tk.GROOVE)
                frame.place(x = 10, y = 50)
                directory = ImageTk.PhotoImage(Image.open('/home/olivier/git/linuxAssistant/resources/images/directory.jpg'))
                self.dirLabels.append(tk.Label(frame, image = directory, text = "plop", compound = tk.CENTER))
                self.dirLabels[nbFiles].place(x=x, y=y)
                frame.update()
                print("x : {}".format(x))
                print("y : {}".format(y))
                nbFiles += 1
                if nbFiles % 10 == 0:
                    x = 0
                    y += 60
                else:
                    x += 60

def run(self):
    print("starting gui")
    self.root.after(200, self.runLoop)
    self.root.mainloop()
    print("gui started")

def quit(self):
    self.root.after(0, self.root.destroy)

因此,我正在创建一个只有1个标签的窗口,并使用self.root.mainloop()启动tkinter。 我还使用root.after不断用runLoop(self)更新窗口

此runLoop正在调用printCurrentDir函数,该函数应为找到的每个目录显示一个带有图标的标签

正确显示tk.Frame is create,但是标签一创建就消失了。 因此,当我运行代码时,我看到第一个标签,然后它消失,而第二个显示。。。 所有标签都已创建,但没有一个标签保留在屏幕上

如果不使用frame.update或self.root.update,则根本不会显示标签 如果我将其替换为self.root.mainloop(),代码将在其上停止,这似乎很正常

当第二个线程需要时,会更新列表内容(config.currentDirContent)和config.currentDirChanged值

整个类在另一个类的线程函数中启动。 我对另一个tkinter类使用相同的行为,没有问题

为了便于理解,在添加了第一个和第二个标签后,这里有两个屏幕截图:

1rst label added

Second label added

在第二次捕获时,我应该有两个标签

第二个问题,我的标签是用图像和文本创建的(plop用于测试)。但是,不会显示文本

你知道我为什么会有这种行为,以及如何修复它吗


Tags: importselfconfigformatdefroot标签frame
1条回答
网友
1楼 · 发布于 2024-10-02 10:26:30

printCurrentDir()函数内for循环的每次迭代中都创建了新的帧(内部有一个标签),并将帧放在相同的位置。因此,最后一帧将覆盖在所有先前创建的帧的顶部。您应该在for循环外部创建框架

同样,在for循环内部创建相同的映像directory,该映像也应该在for循环外部创建,并将其更改为实例变量self.directory,以避免垃圾收集

    def printCurrentDir(self):
        if config.currentDirChanged or not self.currentDirDisplayed:
            print("showing dir")
            self.currentDirDisplayed = True
            nbFiles = 0;
            x = 0
            y = 50

            frame = tk.Frame(self.root, width=600, height=600, bd=2, relief=tk.GROOVE)
            frame.place(x=10, y=50)
            self.directory = ImageTk.PhotoImage(file='/home/olivier/git/linuxAssistant/resources/images/directory.jpg')
            for f in config.currentDirContent:
                if os.path.isdir(f):
                    print("file : {}".format(f))
                    print("nb labels : {}".format(len(self.dirLabels)))
                    self.dirLabels.append(tk.Label(frame, image=self.directory, text="plop", compound=tk.CENTER))
                    self.dirLabels[nbFiles].place(x=x, y=y)
                    #frame.update()
                    print("x : {}".format(x))
                    print("y : {}".format(y))
                    nbFiles += 1
                    if nbFiles % 10 == 0:
                        x = 0
                        y += 60
                    else:
                        x += 60

相关问题 更多 >

    热门问题