python中ttk_ttk/Python中的按钮图像问题

PhotoImage与garbage collector有问题,如果图像被分配给局部变量,它会删除图像。在

将image赋值给类变量self.buttonPhotoself.buttonPhoto = ImageTk.PhotoImage(buttonImage)

完整:

^{pr2}$

或者将图像分配给其他对象-它也应该停止garbage collector。在

也就是说myButton.image = buttonPhoto

完整:from tkinter import *

from tkinter import ttk

from PIL import Image, ImageTk

class Example(ttk.Frame):

def __init__(self, master):

ttk.Frame.__init__(self, master)

master.title('Button Test')

self.configure(padding='10 10 10 10')

self.grid(column=0, row=0, sticky=(N, E, W, S))

buttonImage = Image.open('tplay.png')

buttonPhoto = ImageTk.PhotoImage(buttonImage)

myButton = ttk.Button(self, image=buttonPhoto, padding='10 10 10 10')

myButton.grid(column=1, row=1, sticky=(E, W))

# assign image to other object

myButton.image = buttonPhoto

if __name__ == "__main__":

root = Tk()

example = Example(root)

root.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值