简体   繁体   English

如何获取已分配给小部件的图像变量?

[英]How to get the image variable that has been assigned to a widget?

How do I get the image variable of the button myBtn .如何获取按钮myBtn的图像变量。

from tkinter import *

master = Tk()
FiveStarsImg = PhotoImage(file=r"D:\Users\Jean Paul\OneDrive\Programming\JP\Programs\Prog 7 - Generals Online Game\Characters\1- Five stars.png")

myBtn = Button(master, image=FiveStarsImg)
master.mainloop()

If I print myBtn['image'] it just returns pyimage , but I need it to return the image variable name FiveStarsImg .如果我打印myBtn['image']它只返回pyimage ,但我需要它返回图像变量名称FiveStarsImg

How would I do this?我该怎么做?

I am not aware of any method to return the actual variable name but if you store all your values in a list you can always reference them by index or iterate over the stored name.我不知道有任何方法可以返回实际变量名称,但如果您将所有值存储在列表中,您始终可以通过索引引用它们或遍历存储的名称。

Here is an example.这是一个例子。

I have 4 images stored in a folder called images.我有 4 张图像存储在一个名为 images 的文件夹中。

I store the index, file path, image, button and label in a list of list.我将索引、文件路径、图像、按钮和标签存储在列表中。 I use a lambda to hold the index of this grouping of information so we can reference the stored list to compare values.我使用 lambda 来保存这组信息的索引,以便我们可以引用存储的列表来比较值。

In this case I made a function that will be used to check if each image is the same size as the button clicked.在这种情况下,我创建了一个函数,用于检查每个图像是否与单击的按钮大小相同。

Here are the images if you want to test with the same ones I did.如果您想使用与我相同的图像进行测试,这里是图像。

暗红色 - Copy.png 暗红色.png 灰色.png graybg.png

Here is the code.这是代码。 Make sure to either change the directory of images being search or make a directory for images.确保更改正在搜索的图像目录或为图像创建目录。

import tkinter as tk
import os
import cv2


master = tk.Tk()
image_list = []


def check_all(i):
    for image_group in image_list:
        im1 = cv2.imread(image_list[i][1])
        im2 = cv2.imread(image_group[1])
        if im1.shape == im2.shape:
            image_group[4]['text'] = f"image matches size {image_list[i][1]}"
        else:
            image_group[4]['text'] = f"does not match size"

ndex = 0
for filename in os.scandir(r".\images"):
    if filename.is_file():
        img = tk.PhotoImage(file=filename.path)
        image_list.append([ndex, filename.path, img,
                           tk.Button(master, image=img, command=lambda i=ndex: check_all(i)),
                           tk.Label(master, text='Label for checking images against each other')])
        image_list[-1][3].grid(row=ndex, column=0)
        image_list[-1][4].grid(row=ndex, column=1)
        ndex += 1


master.mainloop()

Results:结果:

For example if I select the 1st image button the label to the right of the 1st button will change to "image matches size" where the other 3 will show "does not match size"例如,如果我选择第一个图像按钮,第一个按钮右侧的标签将更改为“图像匹配大小”,而其他 3 个将显示“大小不匹配”

在此处输入图像描述

If I select the 2nd button it will match size for the 2nd and 3rd buttons because both of those images are the same dimensions.如果我选择第二个按钮,它将匹配第二个和第三个按钮的大小,因为这两个图像的尺寸相同。

在此处输入图像描述

All of this is to illustrate that you can store everything in a list to manage buttons dynamically and/or use the stored images to compare later down the road in your code.所有这些都是为了说明您可以将所有内容存储在列表中以动态管理按钮和/或使用存储的图像在以后的代码中进行比较。

Let me know if you have any questions.如果您有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM