Tkinter添加圖片

Tkinter添加圖片的方式,與Java類似都是利用label標籤來完成的:oop

1、默認的是gif的格式,注意將png後綴名修改成gif仍是沒法使用,文件格式依然錯誤spa

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

2、.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()

 

  結果呈現:
blog

 

3、注意
使用的時候先添加背景圖片在來進行其餘組件的配置


圖片

相關文章
相關標籤/搜索