返回列表 發帖

圖形介面 (五) - place排版練習

  1. import tkinter as tk
  2. win=tk.Tk()
  3. win.title("主視窗")
  4. win.geometry("500x500")
  5. win.resizable(1,1)
  6. lb=tk.Label(win,bg="#FFFF77",fg="red",text="這是絕對位置,下面是相對位置。",font=("微軟正黑體",16)).place(x=50, y=10)  #anchor的預設值為nw
  7. lb=tk.Label(win,bg="blue").place(relx=0.5, rely=0.5, relwidth=0.7, relheight=0.7, anchor="center")
  8. lb=tk.Label(win,bg="yellow").place(relx=0.5, rely=0.5, relwidth=0.5, relheight=0.5, anchor="center")
  9. lb=tk.Label(win,bg="red").place(relx=0.5, rely=0.5, relwidth=0.3, relheight=0.3, anchor="center")
  10. lb=tk.Label(win,bg="green").place(relx=0.5, rely=0.5, relwidth=0.1, relheight=0.1, anchor="center")
  11. win.mainloop()
複製代碼
屬性        說明
-----------------------------------------------------------------
relx         設定元件橫向相對位置,參數值在0與1之間
rely         設定元件縱向相對位置,參數值在0與1之間
relwidth   設定元件的相對寬度,參數值在0與1之間
relheight  設定元件的相對高度,參數值在0與1之間
anchor     設定元件位置基準點,有:center、n、s、w、e、nw、nw、sw、se

  1. import tkinter as tk
  2. win=tk.Tk()
  3. win.title("主視窗")
  4. win.geometry("500x500")
  5. win.resizable(1,1)
  6. lb=tk.Label(win,bg="yellow",fg="red",text="這是絕對位置,下面是相對位置",font=("微軟正黑體",16)).place(x=50,y=10)
  7. lb=tk.Label(win,bg="blue").place(relx=0.5,rely=0.5,relwidth=0.7,relheight=0.7,anchor="center")
  8. lb=tk.Label(win,bg="green").place(relx=0.5,rely=0.5,relwidth=0.5,relheight=0.5,anchor="center")
  9. lb=tk.Label(win,bg="yellow").place(relx=0.5,rely=0.5,relwidth=0.3,relheight=0.3,anchor="center")
  10. lb=tk.Label(win,bg="red").place(relx=0.5,rely=0.5,relwidth=0.1,relheight=0.1,anchor="center")
  11. win.mainloop()
複製代碼

TOP

  1. import tkinter as tk
  2. win=tk.Tk()
  3. win.title("主視窗")
  4. win.geometry("500x500")
  5. win.resizable(1,1)
  6. lb=tk.Label(win,bg="blue").place(relx=0.5, rely=0.5, relwidth=0.7, relheight=0.7, anchor="center")
  7. lb=tk.Label(win,bg="yellow").place(relx=0.5, rely=0.5, relwidth=0.5, relheight=0.5, anchor="center")
  8. lb=tk.Label(win,bg="red").place(relx=0.5, rely=0.5, relwidth=0.3, relheight=0.3, anchor="center")
  9. lb=tk.Label(win,bg="green").place(relx=0.5, rely=0.5, relwidth=0.1, relheight=0.1, anchor="center")
  10. win.mainloop()
複製代碼

TOP

返回列表