本文介绍了为 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:关键词]
发布评论