首页 > 解决方案 > 更新类外的类标签

问题描述

def check_file_type():
    test = Gui()
    test.info_label['text'] = ''
    inputpath = r'C:\...'
    files = (file for file in os.listdir(inputpath) if os.path.isfile(os.path.join(inputpath, file)))
    for file in files:
        if file.endswith('.pdf'):
            pass
        else:
            jpg_to_png(file)

...

class Gui():
    def __init__(self):
        self.gui = tk.Tk()
        self.gui.title('Smart Archive')
        self.gui.geometry('500x500')

        self.scan_png_button = tk.Button(self.gui, text='Scan files', relief='groove', command = check_file_type())
        self.scan_png_button.place(x=15, y=15)
        self.info_label = tk.Label(self.gui, text='On Hold')
        self.info_label.place(x=15, y=40)

我正在尝试使用 Gui().info_lable['text'] 从外部类更新“self.info_label”,但出现此错误:

RecursionError: maximum recursion depth exceeded while calling a Python object

所以...有没有一种方法可以更新类外的标签?

标签: pythontkinter

解决方案


使用 .config() 并更改您想要的功能,就像这里我将文本更改为“”。从按钮中的命令调用函数时也不要放 ()。

def check_file_type():

    app.info_label.config(text='')
    inputpath = r'C:\...'
    files = (file for file in os.listdir(inputpath) if os.path.isfile(os.path.join(inputpath, file)))
    for file in files:
        if file.endswith('.pdf'):
            pass
        else:
            jpg_to_png(file)


class Gui():
    def __init__(self):
        self.gui = tk.Tk()
        self.gui.title('Smart Archive')
        self.gui.geometry('500x500')

        self.scan_png_button = tk.Button(self.gui, text='Scan files', relief='groove', command = check_file_type)
        self.scan_png_button.place(x=15, y=15)
        self.info_label = tk.Label(self.gui, text='On Hold')
        self.info_label.place(x=15, y=40)

app=Gui()

推荐阅读