xia的小窩

一起來coding和碼字吧

0%

標籤label

基本應用

建立第一個標籤

1
2
3
4
5
6
7
8
9
10
from tkinter import *

root = Tk()
root.title("python-tkinter")
root.geometry("500x300")
label = Label(root, text = "男人的嘴,騙人的鬼")
label.pack()
# 包裝與定位
root.configure(bg = "green")
root.mainloop()

結果

Widget共通屬性 - Color顏色

將文字背景色調整,文字的部分也跟著調整

1
label = Label(root, text = "男人的嘴,騙人的鬼", fg = "blue", bg = "yellow")

結果

Widget共通屬性 - Dimensions大小

內部擴充

1
2
label = Label(root, text = "男人的嘴,騙人的鬼", fg = "blue", bg = "yellow", 
height = 5, width = 15)

結果

Widget共通屬性 - Anchor錨

上下左右對齊,使用(nw, n, ne)…..等

1
2
label = Label(root, text = "男人的嘴,騙人的鬼", fg = "blue", bg = "yellow", 
height = 5, width = 15, anchor = "s")

結果

Label文字輸出換行 – wraplength

在40像素後自動換行

1
2
3
label = Label(root, text = "男人的嘴,騙人的鬼", fg = "blue", bg = "yellow", 
height = 5, width = 15, anchor = "s",
wraplength = 40)

結果

Widget共通屬性 – Font字型

調整字體大小,字型等…….

1
2
3
label = Label(root, text = "男人的嘴,騙人的鬼", fg = "blue", bg = "yellow", 
height = 5, width = 15, anchor = "s",
font = "Helvetica 20 bold")

結果

Label的justify參數

靠左靠右置中

1
2
3
4
# 靠左
label = Label(root, text = "abcdefghijklmnop", fg = "blue", bg = "yellow",
wraplength = 80,
justify = "left")

結果

Widget共通屬性 – Bitmaps

使用內建位元圖

1
label = Label(root, bitmap = "error")

結果

compound參數

圖像位置 :point_down:

1
2
label = Label(root, text = "男人的嘴,騙人的鬼" , bitmap = "error"
, compound = "right")

結果

Widget共通屬性 – Relief style

管控邊框

1
label = Label(root, text = "男人的嘴,騙人的鬼", relief = "raised")

結果

標籤與文字的區間

指的是剛才所用的……內部文字的邊框的距離

1
2
label = Label(root, text = "男人的嘴,騙人的鬼", relief = "raised"
, padx = 5, pady = 10)

結果

padx指的是左右間距,pady指的是上下間距

影像

我們這邊就寫成同時出現文字和圖片吧~~
先安裝模組

1
pip install pillow

然後開始作業

1
2
3
4
5
6
from PIL import Image, ImageTk

image = Image.open("ball.jpg")
Ball = ImageTk.PhotoImage(image)

label = Label(root, text = "男人的嘴,騙人的鬼", image = Ball, justify = "left", compound = "right")

結果

Widget共通方法 – config()

可以直接設定物件屬性,如果部分屬性未設定,後面想要更改屬性可以使用config()方法

計時器 :point_down:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from tkinter import *

counter = 0
def run_counter(label):
def counting():
global counter
counter += 1
label.config(text = str(counter))
label.after(1000, counting)
counting()

root = Tk()
root.title("python-tkinter")
root.geometry("300x300")
root.configure(bg = "green")


label = Label(root, height = 30, width = 50,justify = "left", compound = "right")
label.pack()
run_counter(label)

root.mainloop()

結果

Widget共通屬性 – Cursors

cursors是滑鼠外形

1
2
label = Label(root, text = "男人的嘴,騙人的鬼", height = 30, width = 30, 
justify = "left", compound = "right", cursor = "heart")

無法截圖,所以自己先確認是否出現了愛心滑鼠~

Widget共通方法 – keys()

列出所有label參數

1
paint(label.keys())

結果

分割線 – Separator

整體視覺

1
2
3
4
5
6
7
8
9
10
11
12
13
from tkinter.ttk import Separator


label_1 = Label(root, text = "For the test",
justify = "left", compound = "right", cursor = "heart")
label_1.pack(padx = 10, pady = 10)

sep = Separator(root, orient = HORIZONTAL)
sep.pack(fill = X, padx = 5)

label_2 = Label(root, text = "男人的嘴,騙人的鬼",
justify = "left", compound = "right", cursor = "heart")
label_2.pack(padx = 10, pady = 10)

結果

ttk

其實主要差別還在在於美觀上。

1
2
3
4
5
6
7
8
9
from ttkbootstrap.constants import *
import ttkbootstrap as ttk
import tkinter as tk

root = tk.Tk()
root.title('tk + ttk')
root.configure(bg = "green")
root.geometry("300x200+100+100")
root.mainloop()

這是示意圖。
image