【问题标题】:How to display multiple images in tkinter using loop?如何使用循环在 tkinter 中显示多个图像?
【发布时间】:2021-10-12 02:03:26
【问题描述】:

我想使用 for 循环在我的 tkinter 窗口上显示多个图像。 该窗口仅显示最后一张图像。我该如何解决? 我对 tkinter 和 python 有点陌生。 以下是我的代码:

mycursor.execute("SELECT * from Contacts ORDER BY NAME")

show_result = mycursor.fetchall()

name_list = []
contact_list = []
email_list = []
dob_list = []
category_list = []
profile_location = []

for row in show_result:
    name_list.append(row[0])
    contact_list.append(row[1])
    email_list.append(row[2])
    dob_list.append(row[3])
    category_list.append(row[4])
    profile_location.append(row[5])

name_length = len(name_list)

a = 200

b = 30
c = 50

imgx = 10
imgy = 20


for i in range(0, name_length):
    name = name_list[i]
    contact = contact_list[i]
    email = email_list[i]
    dob = dob_list[i]
    category = category_list[i]
    profile = profile_location[i]

    img_open = Image.open(profile)
    img_tst = ImageTk.PhotoImage(img_open)

    label_name = Label(seewindow, image=img_tst)
    label_name.place(x=imgx, y=imgy)

    a += 200
    imgx += 250

【问题讨论】:

  • 尝试添加label_name.image=img_tst
  • @Sujay 不会有帮助,因为label_name 已被覆盖,因此从技术上讲它超出了范围
  • 将所有img_tsts 放在一个列表中。
  • @Sujay 谢谢你这个作品
  • @TheLizzard 它以某种方式工作,我的问题得到了解决,谢谢

标签: python image loops tkinter


【解决方案1】:

尝试在label_name.place之后添加。局部变量被垃圾收集。使用

保存照片的参考
label_name.image=img_tst

【讨论】:

  • 如果name_length 大于1,则变量label_name 将被覆盖。这会导致旧的label_name 被垃圾回收。因此,这不是一个适用于大多数情况的解决方案。
猜你喜欢
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 2011-06-08
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多