简体   繁体   中英

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

How do I get the image variable of the button 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 .

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.

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.

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"

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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