【问题标题】:cget('image') method returning pyimage1 instead of the image namecget('image') 方法返回 pyimage1 而不是图像名称
【发布时间】:2013-03-01 22:13:48
【问题描述】:

我是编程新手,我正在尝试使用 python 3.3 中的 tkinter 创建一个应用程序。在此应用程序中,我正在使用包含图像的按钮,并且我想执行取决于按钮包含的图像类型的操作。这是我的程序的简化版本:

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

我也不知道为什么。我已经想弄清楚几个小时了。也许有一种不同的方法?

【问题讨论】:

    标签: python widget tkinter


    【解决方案1】:

    使用cget(),您只能将属性作为字符串检索,因此您需要存储对PhotoImage 对象的引用:

    b = Button(...)
    b.image = c_black
    print(b.image.cget('file'))
    

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多