tkinter第二章(添加圖片,背景圖片)


#插入文件圖片
import tkinter as tk

root = tk.Tk()

#創建一個標簽類, [justify]:對齊方式
textLabel = tk.Label(root,text="你在右邊會看到一個圖片,\n我在換個行",
justify = tk.LEFT)#左對齊
textLabel.pack(side=tk.LEFT)#自動對齊,side:方位

 

#創建一個圖片管理類
photo = tk.PhotoImage(file="18.png")#file:t圖片路徑
imgLabel = tk.Label(root,image=photo)#把圖片整合到標簽類中
imgLabel.pack(side=tk.RIGHT)#自動對齊


tk.mainloop()

 

 

 

import tkinter as tk

root = tk.Tk()


#增加背景圖片
photo = tk.PhotoImage(file="背景.png")
theLabel = tk.Label(root,
         text="我是內容,\n請你閱讀",#內容
         justify=tk.LEFT,#對齊方式
         image=photo,#加入圖片
         compound = tk.CENTER,#關鍵:設置為背景圖片
         font=("華文行楷",20),#字體和字號
         fg = "white")#前景色
theLabel.pack()

 

tk.mainloop()

 

#插入文件圖片
import tkinter as tk

root = tk.Tk()

frame1 = tk.Frame(root)#這是上面的框架
frame2 = tk.Frame(root)#這是下面的框架


var = tk.StringVar()#儲存文字的類
var.set("你在右邊會看到一個圖片,\n我在換個行")#設置文字

#創建一個標簽類, [justify]:對齊方式,[frame]所屬框架
textLabel = tk.Label(frame1,textvariable=var,
         justify = tk.LEFT)#顯示文字內容
textLabel.pack(side=tk.LEFT)#自動對齊,side:方位

 

#創建一個圖片管理類
photo = tk.PhotoImage(file="18.png")#file:t圖片路徑
imgLabel = tk.Label(frame1,image=photo)#把圖片整合到標簽類中
imgLabel.pack(side=tk.RIGHT)#自動對齊


def callback():#觸發的函數
  var.set("你還真按了")#設置文字

#[frame]所屬框架 ,text 文字內容 command:觸發方法
theButton = tk.Button(frame2,text="我是下面的按鈕",command=callback)
theButton.pack()#自動對齊

 

frame1.pack(padx=10,pady=10)#上框架對齊
frame2.pack(padx=10,pady=10)#下框架對齊


tk.mainloop()

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM