问题描述
我是编程新手,正在尝试使用 python 3.3 中的 tkinter 创建应用程序.在这个应用程序中,我使用包含图像的按钮,我想根据按钮所包含的图像类型执行操作.这是我的程序的简化版本:
I'm new to programming and I'm attempting to create an application using tkinter from python 3.3. In this application I'm using buttons containing images and I want to perform actions that depend on the kind of image that the buttons are containing. This is a simplified version of my program:
from tkinter import *
master=Tk()
c_black = PhotoImage(file="c_black.gif")
b=Button(master, image=c_black)
print(b.cget('image'))
master.mainloop()
代替
c_black
控制台返回
pyimage1
我不知道为什么.几个小时以来,我一直试图弄清楚.也许有一种不同的方法?
And I have no idea why. I've been trying to figure it out for hours now. Perhaps there's a way to do it differently?
推荐答案
使用 cget()
只能以字符串形式检索属性,因此需要存储对 的引用PhotoImage
对象:
With cget()
you can only retrieve the property as a string, so you need to store the reference to the PhotoImage
object:
b = Button(...)
b.image = c_black
print(b.image.cget('file'))
这篇关于cget('image') 方法返回 pyimage1 而不是图像名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!