Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

python - 如何在窗口中动态插入随机数量的图像(How to dynamically insert a random number of images into the window)

I have the following code:

(我有以下代码:)

import tkinter as tk

data = [['A',1],['B',2],['C',3],['D',4]]

def ranking():
    root = tk.Tk() 
    root.focus_force() 

    windowWidth = root.winfo_reqwidth()
    windowHeight = root.winfo_reqheight()
    positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2)
    positionDown = int(root.winfo_screenheight()/2 - windowHeight/2)
    root.geometry("+{}+{}".format(positionRight, positionDown))

    for i in range(0,len(data)):
        tk.Label(image=tk.PhotoImage(file="D:\A\" + data[i][0] + ".gif")).grid(row=data[i][1]-1, column=0)

    root.mainloop()

ranking()

What I want to do is to have a random number of pictures (whatever is inserted in data at any moment), displayed within the window (in the order as indicated within the data), yet if I currently run the code it simply results in a blank window.The names of the pictures are literally A, B, C...and they display without a problem if I use something like (this is an extract from another piece of code I wrote):

(我想做的是在窗口内(按数据中指示的顺序)显示随机数量的图片(无论何时将其插入到数据中),但是如果我当前运行代码,它只会导致图片的名称实际上是A,B,C ...,如果我使用类似的图片,它们的显示就没有问题(这是我编写的另一段代码的摘录):)

img2 = tk.PhotoImage(file="D:\A\" + choice2 + ".gif")
label_img2 = tk.Label(image=img2)
label_img2.grid(row=2, column=1)

Any guidance would be very welcome as I am still quite new with working with tkinter!

(任何指导都将受到欢迎,因为与tkinter合作对我来说还很新!)

  ask by Noir translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you add a PhotoImage or other Image object to a Tkinter widget, you must keep your own reference to the image object.

(将PhotoImage或其他Image对象添加到Tkinter小部件时,必须保留自己对image对象的引用。)

If you don't, the image won't always show up.

(如果您不这样做,图像将不会总是显示。)

The problem is that the Tkinter/Tk interface doesn't handle references to Image objects properly;

(问题在于Tkinter / Tk接口无法正确处理对Image对象的引用;)

the Tk widget will hold a reference to the internal object, but Tkinter does not.

(Tk小部件将保留对内部对象的引用,但Tkinter不会。)

When Python's garbage collector discards the Tkinter object, Tkinter tells Tk to release the image.

(当Python的垃圾回收器丢弃Tkinter对象时,Tkinter告诉Tk释放图像。)

But since the image is in use by a widget, Tk doesn't destroy it.

(但是由于小部件正在使用该图像,因此Tk不会销毁它。)

Not completely.

(不完全的。)

It just blanks the image, making it completely transparent…

(它只是使图像空白,使其完全透明...)

The solution is to make sure to keep a reference to the Tkinter object, for example by attaching it to a widget attribute:

(解决方案是确保保留对Tkinter对象的引用,例如,将其附加到widget属性:)

for i in range(0,len(data)):
    photo = tk.PhotoImage(file="D:\A\" + data[i][0] + ".gif")
    label = tk.Label(image=photo )
    label.image = photo  # keep a reference!
    label.grid(row=data[i][1]-1, column=0)

Taken from Why do my Tkinter images not appear?

(取自为什么我的Tkinter图像不出现?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...