1. 程式人生 > >tkinter第二章(添加圖片,背景圖片)

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

src 分享 標簽 file var cal 添加 main int

#插入文件圖片
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()

技術分享

技術分享

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