Tkinter添加图片的方式,与Java相似都是利用label标签来完成的:

一、默认的是gif的格式,注意将png后缀名修改为gif还是无法使用,文件格式依然错误

photo = PhotoImage(file=r'【文件名】.gif')
label = Label(【Tk对象】, image=photo)

二、.png文件的格式
利用Pillow库导入,ImageTk。
photo = ImageTk.PhotoImage(file=r'【文件名】.png')
label = Label(【Tk对象】, image=photo)
example:

 1 from tkinter import *
 2 
 3 root = Tk()
 4 photo = PhotoImage(file=".\images\element_1.png")
 5 Label(root, image=photo).pack()
 6 
 7 
 8 def callback():
 9     print('点到我了')
10 
11 
12 Button(root, text='点我', command=callback).place(relx=0.5, rely=0.5, anchor=CENTER)  # relx和rely是相对父组件的位置,范围是 00~1.0。0是最左边,0.5是正中间,1是最右边
13 mainloop()

 

  结果呈现:

 

三、注意
使用的时候先添加背景图片在来进行其他组件的配置


posted on 2019-02-12 22:15  蔚蓝色の天空  阅读(3354)  评论(0编辑  收藏  举报