使用tkinter在for标签中显示for语句,是否可以完成? [英] Displaying pics in Labels with a for Statement using tkinter, can it be done?
问题描述
从tkinter导入*
从tkinter导入ttk
导入随机
root = Tk()
root.title(RandomizedPic)
def randp(* args):
w = ['wb.gif','wc.gif' ,'wd.gif','we.gif']
random.shuffle(w)
am = 1
for w:
pic = PhotoImage (file = i)
ttk.Label(mainframe,image = pic).grid(column = am,row = 0,sticky =(W,E))
am + = 1
mainframe = ttk.Frame(root,padding =3 3 12 12)
mainframe.grid(column = 0,row = 0,sticky =(N,W,E,S ))
mainframe.columnconfigure(0,weight = 1)
mainframe.rowconfigure(0,weight = 1)
ttk.Button(mainframe,text =Do it ,command = randp).grid(column = 0,row = 0,sticky = W)
root.bind('< Return>',randp )
root.mainloop()
如何让这个工作的任何建议将是非常感谢。
这是tkinter的一个众所周知的问题 - 你必须保留自己的所有Photoimages的引用,否则python将垃圾收集他们 - 这就是发生在您的图像。仅仅将它们设置为标签图像不会增加图像对象的引用计数。
解决方案:
为了解决这个问题,你需要持久地引用你创建的所有图像对象。理想情况下,这将是一个类名称空间中的数据结构,但由于您没有使用任何类,所以模块级别的列表将不得不做:
图片= [无,无,无,无]#这将是将保存每个PhotoImages引用的列表。
def randp(* args):
w = ['wb.gif','wc.gif','wd.gif','we.gif']
random .wuffle(w)
am = 1
为枚举(w)中的k,i:#Enumerate为图片列表提供索引。
pic = PhotoImage(file = i)
pics [k] = pic#保留对列表中PhotoImage的引用,所以PhotoImage不会被垃圾收集。
ttk.Label(mainframe,image = pic).grid(column = am,row = 0,sticky =(W,E))
am + = 1
图片= [无,无,无,无]#这将是将保存每个PhotoImages引用的列表。
def randp(* args):
w = ['wb.gif','wc.gif','wd.gif','we.gif']
random .wuffle(w)
am = 1
为枚举(w)中的k,i:#Enumerate为图片列表提供索引。
pic = PhotoImage(file = i)
pics [k] = pic#保留对列表中PhotoImage的引用,所以PhotoImage不会被垃圾收集。
ttk.Label(mainframe,image = pic).grid(column = am,row = 0,sticky =(W,E))
am + = 1
I am trying to get a random bunch of pictures to print side by side; the problem is that if I run the following code all that happens is that it creates a group of blank empty labels. If I replace the 'image=pic' with a "text='whatever'" it works fine (thus proving that it dows actually create the label). Placing the label and image anywhere else works fine (proving that it's not the images), even if I use 'pic = PhotoImage(file=w[0])' it works (so I don't think its my method)...
from tkinter import *
from tkinter import ttk
import random
root = Tk()
root.title("RandomizedPic")
def randp(*args):
w = ['wb.gif', 'wc.gif', 'wd.gif', 'we.gif']
random.shuffle(w)
am = 1
for i in w:
pic = PhotoImage(file=i)
ttk.Label(mainframe, image=pic).grid(column=am, row=0, sticky=(W, E))
am+=1
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
ttk.Button(mainframe, text="Do it", command=randp).grid(column=0, row=0, sticky=W)
root.bind('<Return>', randp)
root.mainloop()
Any advice on how to get this to work will be much appreciated.
This is a well-known issue with tkinter - you MUST keep your own reference to all Photoimages, otherwise python will garbage collect them - that's what's happening to your images. Merely setting them as the image for a label does NOT add to the reference count for the image objects.
SOLUTION:
To solve this problem, you'll need a persistent reference to all the image objects you create. Ideally, this would be a data structure in a class namespace, but since you aren't using any classes, a module-level list will have to do:
pics = [None, None, None, None] # This will be the list that will hold a reference to each of your PhotoImages.
def randp(*args):
w = ['wb.gif', 'wc.gif', 'wd.gif', 'we.gif']
random.shuffle(w)
am = 1
for k, i in enumerate(w): # Enumerate provides an index for the pics list.
pic = PhotoImage(file=i)
pics[k] = pic # Keep a reference to the PhotoImage in the list, so your PhotoImage does not get garbage-collected.
ttk.Label(mainframe, image=pic).grid(column=am, row=0, sticky=(W, E))
am+=1
这篇关于使用tkinter在for标签中显示for语句,是否可以完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!