python png格式的图片做button

一、png格式的图片
import tkinter
from PIL import Image, ImageTk

root = tkinter.Tk()
img = Image.open("timg1_new.png")
img_ = ImageTk.PhotoImage(img)
button = tkinter.Button(root, image=img_, width=100, height=100)
button.place(x=10, y=10)

root.mainloop()
'''
注意事项:
1、图片格式为.png
2、图片长宽大小要与button长宽一致
3、img_ 定义为全局变量。若不是全局变量,可能图片不会显示。
'''

posted @ 2019-12-26 17:36  交流(Communication)  阅读(1365)  评论(0编辑  收藏  举报