【问题标题】:Attempting to make a photo slide尝试制作照片幻灯片
【发布时间】: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()


【问题讨论】:

  • 你能让它重现吗?否则真的很难找到。

标签: python tkinter


【解决方案1】:

看起来您正在尝试更新 GUI 上的图像。但是,似乎在初始设置后图像没有更新在里面方法。

一个问题可能是每次调用 change_photo 方法时您都在创建 ImageTk.PhotoImage 的新实例。但是,它没有在任何地方被引用,所以之前的实例正在被垃圾收集,这就是图像没有显示在 GUI 上的原因。

尝试将 ImageTk.PhotoImage 实例存储在类变量中并用它更新标签。像这样:

class Window(Frame):
    def __init__(self, master=None):
        ...
        self.imageTK = ImageTk.PhotoImage(self.image)
        self.label = Label(self, image=self.imageTK)
        ...

    def change_photo(self):
        if self.index == (len(self.photos) - 1):
            self.index = 0
        else:
            self.index += 1
            self.img_copy = Image.open(path + self.photos[self.index])
            self.imageTK = ImageTk.PhotoImage(self.img_copy)
            self.label.configure(image=self.imageTK)
            root.after(1000, self.change_photo)

另一个问题是您没有在 change_photo 方法中调用 resize_image。您应该在更新标签后调用它。

def change_photo(self):
    if self.index == (len(self.photos) - 1):
        self.index = 0
    else:
        self.index += 1
        self.img_copy = Image.open(path + self.photos[self.index])
        self.resize_image(self.label)
        root.after(1000, self.change_photo)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    相关资源
    最近更新 更多