python - 更新类外的类标签
问题描述
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
所以...有没有一种方法可以更新类外的标签?
解决方案
使用 .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()
推荐阅读
- javascript - 如何通过单击具有幻灯片效果的锚点而不是直接加载页面来打开网站中的另一个页面?
- c++ - 如何从外部进程访问视频帧?
- r - 根据公共 ID 将行连接成一行
- java - Vaadin 行的网格编号
- batch-file - 在批处理文件中使用 7zip“排除通配符”
- r - 使用 downloadHandler 时如何修复“找不到文件”?
- keras - 使用 Keras LSTM 预测未来值
- javascript - JS Array.prototype.sort() 在 Firefox 中失败,在 Chrome 中有效
- python - 为分类问题添加注意力机制
- arrays - 从数组中的工作表返回值