在 tkinter 中使用循环在框架内添加图像
Add images inside frame with loop in tkinter
这是我的代码的一部分,它根据来自外部文件的 JSON 对象将图像加载到 LabelFrame 中。代码在一个函数中(似乎它也起作用,谁知道呢)。
# the black blackboard where images will be stored
black_background = tk.LabelFrame(pivote_tab, width=1340, height=290, bg="black")
black_background.grid(row=0, column=0, sticky="E")
# open the .json file
json_file = open('path/to/file.json', 'r')
json_data = json_file.read()
#the init position for image inside the blackboard
live_pc_position = 10
# make a loop for every object inside JSON
obj = json.loads(json_data)
for i in range(int(len(obj))):
os = (str(obj[i].get('operating_system')))
if "Mac" in os:
img_src = "image/macos_pc.png"
elif "Win" in os and "10" in os:
img_src = "image/windows_10.png"
elif "Linux" in os:
img_src = "image/linux_pc_image.png"
else:
img_src = "image/undetected_pc.png"
# defining the image based on operating system and store it to the pc_image variable
pc_image = PhotoImage(file=img_src)
# adding the computer icon inside the blackboard
computer_icon = Label(black_background, image=pc_image, bg="black")
computer_icon.place(x=live_pc_position, y=10)
# update values so the next image will load to the right
live_pc_position = live_pc_position + 40
脚本没有出错,但是由于某些原因,当有更多的图像需要加载时,只显示第一张图像,因为 JSON 有更多的对象。
图像未在 tkinter 中加载的问题几乎总是与 this 有关。
在每个循环中替换 pc_image 中的值,python 垃圾收集器丢弃 Tkinter 图像对象。解决方案是将每个 Tkinter 对象存储在不同的变量中,这可以通过对象属性或列表来完成,如下例所示:
pc_images=[]
for i in range(int(len(obj))):
...
pc_images.append(PhotoImage(file=img_src))
Label(black_background, image=pc_images[i], bg="black").place(x=live_pc_position, y=10)
live_pc_position = live_pc_position + 40
来自@flavio-moraes 的脚本在逻辑上是可行的,但是结合我得到的答案,我制作了满足我需求的最终脚本。
pc_images=[]
for i in range(int(len(obj))):
...
pc_images.append(PhotoImage(file=img_src))
live_pc_position = 10
for pc in pc_images:
#create the image
computer_icon = Label(black_background, image=pc, bg="black")
computer_icon.img = pc_images
computer_icon.place(x=live_pc_position, y=10)
# update values so the next image will load to the right
live_pc_position = live_pc_position + 40
这就是我在评论中提出的建议。参见 ALL CAPS COMMENT
。
obj = json.loads(json_data)
for i in range(int(len(obj))):
os = (str(obj[i].get('operating_system')))
...
# defining the image based on operating system and store it to the pc_image variable
pc_image = PhotoImage(file=img_src)
# adding the computer icon inside the blackboard
computer_icon = Label(black_background, image=pc_image, bg="black")
computer_icon.img = pc_image # KEEP A REFERENCE SO IMAGE IS NOT GARBAGE COLLECTED.
computer_icon.place(x=live_pc_position, y=10)
...
这是我的代码的一部分,它根据来自外部文件的 JSON 对象将图像加载到 LabelFrame 中。代码在一个函数中(似乎它也起作用,谁知道呢)。
# the black blackboard where images will be stored
black_background = tk.LabelFrame(pivote_tab, width=1340, height=290, bg="black")
black_background.grid(row=0, column=0, sticky="E")
# open the .json file
json_file = open('path/to/file.json', 'r')
json_data = json_file.read()
#the init position for image inside the blackboard
live_pc_position = 10
# make a loop for every object inside JSON
obj = json.loads(json_data)
for i in range(int(len(obj))):
os = (str(obj[i].get('operating_system')))
if "Mac" in os:
img_src = "image/macos_pc.png"
elif "Win" in os and "10" in os:
img_src = "image/windows_10.png"
elif "Linux" in os:
img_src = "image/linux_pc_image.png"
else:
img_src = "image/undetected_pc.png"
# defining the image based on operating system and store it to the pc_image variable
pc_image = PhotoImage(file=img_src)
# adding the computer icon inside the blackboard
computer_icon = Label(black_background, image=pc_image, bg="black")
computer_icon.place(x=live_pc_position, y=10)
# update values so the next image will load to the right
live_pc_position = live_pc_position + 40
脚本没有出错,但是由于某些原因,当有更多的图像需要加载时,只显示第一张图像,因为 JSON 有更多的对象。
图像未在 tkinter 中加载的问题几乎总是与 this 有关。
在每个循环中替换 pc_image 中的值,python 垃圾收集器丢弃 Tkinter 图像对象。解决方案是将每个 Tkinter 对象存储在不同的变量中,这可以通过对象属性或列表来完成,如下例所示:
pc_images=[]
for i in range(int(len(obj))):
...
pc_images.append(PhotoImage(file=img_src))
Label(black_background, image=pc_images[i], bg="black").place(x=live_pc_position, y=10)
live_pc_position = live_pc_position + 40
来自@flavio-moraes 的脚本在逻辑上是可行的,但是结合我得到的答案,我制作了满足我需求的最终脚本。
pc_images=[]
for i in range(int(len(obj))):
...
pc_images.append(PhotoImage(file=img_src))
live_pc_position = 10
for pc in pc_images:
#create the image
computer_icon = Label(black_background, image=pc, bg="black")
computer_icon.img = pc_images
computer_icon.place(x=live_pc_position, y=10)
# update values so the next image will load to the right
live_pc_position = live_pc_position + 40
这就是我在评论中提出的建议。参见 ALL CAPS COMMENT
。
obj = json.loads(json_data)
for i in range(int(len(obj))):
os = (str(obj[i].get('operating_system')))
...
# defining the image based on operating system and store it to the pc_image variable
pc_image = PhotoImage(file=img_src)
# adding the computer icon inside the blackboard
computer_icon = Label(black_background, image=pc_image, bg="black")
computer_icon.img = pc_image # KEEP A REFERENCE SO IMAGE IS NOT GARBAGE COLLECTED.
computer_icon.place(x=live_pc_position, y=10)
...