这是tkinter的一个众所周知的问题-您必须保留对所有Photoimages的引用,否则python将对其进行垃圾回收-
这就是您的图像所发生的事情。仅将它们设置为标签的图像不会增加图像对象的引用计数。
解:
要解决此问题,您将需要对创建的所有图像对象的持久引用。理想情况下,这将是类命名空间中的数据结构,但是由于您未使用任何类,因此模块级列表将必须执行:
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



