为 tkinter 创建带有可滚动条的图像列表

编程入门 行业动态 更新时间:2024-10-23 21:32:14
本文介绍了为 tkinter 创建带有可滚动条的图像列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I know how to create images and also a scrollable listbox, but when I combined the two elements it does not work. Any ideas to implement it?

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()

它可以查看图像列表如果

It can view list of images if

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

变成这样

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

推荐答案

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

Every image object must keep a reference. The problem can be solve by adding

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

详情可以参考这里.

这篇关于为 tkinter 创建带有可滚动条的图像列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 05:31:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:滚动条   图像   列表   tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!