【问题标题】:Create list of image with scrollable bar for tkinter为 tkinter 创建带有可滚动条的图像列表
【发布时间】:2018-04-17 08:46:25
【问题描述】:

我知道如何创建图像和一个可滚动的列表框,但是当我将这两个元素结合起来时它不起作用。有什么想法可以实现吗?

import tkinter as tk
from PIL import ImageTk, Image


class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        text = tk.Text(self, wrap="none")
        vsb = tk.Scrollbar(orient="vertical", command=text.yview)
        text.configure(yscrollcommand=vsb.set)
        vsb.pack(side="right", fill="y")
        text.pack(fill="both", expand=True)

        for i in range(20):
            # b = tk.Button(self, text="Button #%s" % i)
            photo = tk.PhotoImage(file='img.png')
            photo = photo.subsample(2)

            b = tk.Label(self,image=photo)
            # b.pack(side='bottom',fill='x')
            text.window_create("end", window=b)
            text.insert("end", "\n")

        text.configure(state="disabled")

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

如果可以查看图片列表

b = tk.Label(self,image=photo)

变成这样

b = tk.Label(self,text='test')

【问题讨论】:

  • 显示你的代码。
  • 我不知道你为什么有这么多底片。这是一个很好的问题,对我帮助很大。谢谢!

标签: tkinter


【解决方案1】:

每个图像对象都必须保留一个引用。 问题可以通过添加来解决

b = tk.Label(self,image=photo)
b.image = photo # keep a reference

详情可参考here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多