【发布时间】:2021-08-28 03:01:03
【问题描述】:
我有一个图像(QR 图像),我必须显示它,并且每次都保持刷新它。我尝试了很多解决方案,但都没有奏效,这是我的代码:
def QRDisplayer():
global displayQR #I tried a tutorial wrote like this line, with it or without nothing changes
path = getcwd() + r"\temp\qr.png"
try:
displayQR.configure(image=PhotoImage(file=path))
except: pass #I used try/except to avoid errors incase the image doesn't exists
root.after(1000, QRDisplayer)
#main window:
root = Tk()
loadConfig()
root.title("")
root.resizable(False, False)
root.geometry("700x500")
displayQR = Label(root,image = None) #None bc it doesn't exists yet
displayQR.pack()
QRDisplayer()
if __name__ == "__main__":
root.mainloop()
图像第一次不显示,bc它不存在,然后我必须开始刷新元素,直到阅读后出现照片。而且照片是可变的,所以我一直在阅读文件并显示内容。
我花了 6 个小时来解决这个问题,但没有任何效果,包括:
Python Tkinter Label redrawing every 10 seconds
Tkinter Reloading Window displaying an Image
Tkinter.Misc-class.html#update
Tkinter.Misc-class.html#update_idletasks
我还尝试使用 Label["image"]=... ... 和线程库进行循环。
【问题讨论】:
-
问题似乎是您无法显示图像,因为它还不存在。你希望我们能提出什么建议?我只能想象 2 种可能性……要么显示一个替代图像,上面写着 “正在加载”,要么等到你的图像确实存在。或者做微软的事情并说“图像加载在 4 秒、3 秒、2 秒、8 分钟、8 分钟、1 秒、15 分钟......” ;-)
-
@Mark Setchell 正如我之前提到的,它确实在 while 之后存在