【发布时间】:2023-02-04 08:59:37
【问题描述】:
我对 Python 很陌生。我正在尝试定期更新图像。我四处搜寻,但我仍在努力按照我想要的方式进行这项工作。我只是要粘贴我拥有的整个 .py 文件。
现在,它似乎正在正确增加。我知道 Window 类中的 init 函数只运行一次,因此它正在迭代但实际上并没有更新 ImageTk.PhotoImage 对象。我认为这与我的 resize_image 函数有关,因为在 change_photo 中,当我尝试将标签配置为具有更新索引的新图像时,我在分配的时间后只得到一张空白图像。
我只是觉得我不太在正确的轨道上,需要在正确的方向上轻推。谢谢
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack(fill=BOTH, expand=YES)
self.photos = getPhotos()
self.index = 0
self.image = Image.open(path + self.photos[self.index])
self.img_copy = self.image.copy()
self.imageTK = ImageTk.PhotoImage(self.image)
self.label = Label(self, image=self.imageTK)
self.label.pack(fill=BOTH, expand=YES)
self.label.bind('<Configure>', self.resize_image)
def resize_image(self, event):
orig_width = self.image.width
orig_height = self.image.height
new_width = updateWidth(orig_width, orig_height)
new_height = event.height
self.image = self.img_copy.resize((new_width, new_height))
self.imageTK = ImageTk.PhotoImage(self.image)
self.label.configure(image=self.imageTK)
def change_photo(self):
if self.index == (len(self.photos) - 1):
self.index = 0
else:
self.index += 1
self.label.configure(image=ImageTk.PhotoImage(Image.open(path + self.photos[self.index])))
root.after(1000, self.change_photo)
app = Window(root)
app.pack(fill=BOTH, expand=YES)
app.change_photo()
root.mainloop()
【问题讨论】:
-
你能让它重现吗?否则真的很难找到。