1. 程式人生 > 實用技巧 >Python tkinter之label

Python tkinter之label

1、label的基本屬性

import tkinter
from tkinter import *

if __name__ == '__main__':
    win = tkinter.Tk()  # 視窗
    win.title('南風丶輕語')  # 標題
    screenwidth = win.winfo_screenwidth()  # 螢幕寬度
    screenheight = win.winfo_screenheight()  # 螢幕高度
    width = 500
    height = 300
    x = int((screenwidth - width) / 2)
    y 
= int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 label = Label( master=win, # 父容器 text='標籤', # 文字 bg='yellow', # 背景顏色 fg='red', # 文字顏色 activebackground='pink', # 狀態為active時的背景顏色 activeforeground='
blue', # 狀態為active的文字顏色 relief='flat', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。預設為 FLAT。 bd=3, # 邊框的大小 height=1, # 高度 width=5, # 寬度 padx=1, # 內間距,字型與邊框的X距離 pady=1, # 內間距,字型與邊框的Y距離 state='normal', # 設定狀態 normal、active、 disabled 預設 normal
cursor='arrow', # 滑鼠移動時樣式 arrow, circle, cross, plus... font=('黑體', 20), # 字型 ) label.pack() win.mainloop()

備註:

①如果同時設定了width、height和padx、pady,最後確定大小是根據誰值大選誰

②支援的字型(通過tkinter.font.families獲取)https://www.cnblogs.com/rainbow-tan/p/14043822.html/

③滑鼠樣式選項

values = ["arrow", "circle", "clock", "cross", "dotbox", "exchange", "fleur", "heart", "man", "mouse", "pirate", "plus", "shuttle", "sizing", "spider", "spraycan", "star","target", "tcross", "trek", "watch"]

2、邊框樣式,元件狀態閱覽

import tkinter
from tkinter import *

if __name__ == '__main__':
    win = tkinter.Tk()  # 視窗
    win.title('南風丶輕語')  # 標題
    screenwidth = win.winfo_screenwidth()  # 螢幕寬度
    screenheight = win.winfo_screenheight()  # 螢幕高度
    width = 980
    height = 300
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置

    values = ['flat', 'sunken', 'raised', 'groove', 'ridge', 'solid']
    for index, value in enumerate(values):
        label = Label(
            master=win,  # 父容器
            text=value,  # 文字
            bg='yellow',  # 背景顏色
            fg='red',  # 文字顏色
            activebackground='pink',  # 狀態為active時的背景顏色
            activeforeground='blue',  # 狀態為active的文字顏色
            relief=value,  # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。預設為 FLAT。
            bd=3,  # 邊框的大小
            height=1,  # 高度
            width=10,  # 寬度
            padx=1,  # 內間距,字型與邊框的X距離
            pady=1,  # 內間距,字型與邊框的Y距離
            state='normal',  # 設定狀態 normal、active、 disabled 預設 normal
            cursor='arrow',  # 滑鼠移動時樣式 arrow, circle, cross, plus...
            font=('Yu Gothic Medium', 15),  # 字型
        )
        label.grid(row=0, column=index, padx=10, pady=10)

    values = ['normal', 'active', 'disabled']
    for index, value in enumerate(values):
        label = Label(
            master=win,  # 父容器
            text=value,  # 文字
            bg='yellow',  # 背景顏色
            fg='red',  # 文字顏色
            activebackground='pink',  # 狀態為active時的背景顏色
            activeforeground='blue',  # 狀態為active的文字顏色
            relief='flat',  # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。預設為 FLAT。
            bd=3,  # 邊框的大小
            height=1,  # 高度
            width=10,  # 寬度
            padx=1,  # 內間距,字型與邊框的X距離
            pady=1,  # 內間距,字型與邊框的Y距離
            state=value,  # 設定狀態 normal、active、 disabled 預設 normal
            cursor='arrow',  # 滑鼠移動時樣式 arrow, circle, cross, plus...
            font=('Yu Gothic Medium', 15),  # 字型
        )
        label.grid(row=1, column=index, padx=10, pady=10)

    win.mainloop()

3、可改變文字的標籤

3.1、通過StringVar改變

import tkinter
from tkinter import *


def event():
    print('當前的值:{}'.format(value.get()))
    value.set('新值')


if __name__ == '__main__':
    win = tkinter.Tk()  # 視窗
    win.title('南風丶輕語')  # 標題
    screenwidth = win.winfo_screenwidth()  # 螢幕寬度
    screenheight = win.winfo_screenheight()  # 螢幕高度
    width = 500
    height = 300
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置

    value = tkinter.StringVar()
    value.set('初始值')
    label = Label(
        master=win,  # 父容器
        text='標籤',  # 文字
        bg='yellow',  # 背景顏色
        fg='red',  # 文字顏色
        activebackground='pink',  # 狀態為active時的背景顏色
        activeforeground='blue',  # 狀態為active的文字顏色
        relief='flat',  # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。預設為 FLAT。
        bd=3,  # 邊框的大小
        height=1,  # 高度
        width=5,  # 寬度
        padx=1,  # 內間距,字型與邊框的X距離
        pady=1,  # 內間距,字型與邊框的Y距離
        state='normal',  # 設定狀態 normal、active、 disabled 預設 normal
        cursor='arrow',  # 滑鼠移動時樣式 arrow, circle, cross, plus...
        font=('黑體', 20),  # 字型
        textvariable=value,  # 通過StringVar設定可改變的值
    )
    label.pack()
    Button(win, text='點選', command=event).pack(pady=10)
    win.mainloop()

3.2、通過config重新設定text

import tkinter
from tkinter import *


def event():
    print('當前的值:{}'.format(label.cget('text')))
    label.config(text='新值')


if __name__ == '__main__':
    win = tkinter.Tk()  # 視窗
    win.title('南風丶輕語')  # 標題
    screenwidth = win.winfo_screenwidth()  # 螢幕寬度
    screenheight = win.winfo_screenheight()  # 螢幕高度
    width = 500
    height = 300
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置

    label = Label(
        master=win,  # 父容器
        text='標籤',  # 文字
        bg='yellow',  # 背景顏色
        fg='red',  # 文字顏色
        activebackground='pink',  # 狀態為active時的背景顏色
        activeforeground='blue',  # 狀態為active的文字顏色
        relief='flat',  # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。預設為 FLAT。
        bd=3,  # 邊框的大小
        height=1,  # 高度
        width=5,  # 寬度
        padx=1,  # 內間距,字型與邊框的X距離
        pady=1,  # 內間距,字型與邊框的Y距離
        state='normal',  # 設定狀態 normal、active、 disabled 預設 normal
        cursor='arrow',  # 滑鼠移動時樣式 arrow, circle, cross, plus...
        font=('黑體', 20),  # 字型
    )
    label.pack()
    Button(win, text='點選', command=event).pack(pady=10)
    win.mainloop()

4、顯示圖片的label

tkinter只支援gif圖片,如果使用其他格式圖片,需要使用PIL模組

import tkinter
from tkinter import *
from PIL import Image
from PIL import ImageTk

if __name__ == '__main__':
    win = tkinter.Tk()  # 視窗
    win.title('南風丶輕語')  # 標題
    screenwidth = win.winfo_screenwidth()  # 螢幕寬度
    screenheight = win.winfo_screenheight()  # 螢幕高度
    width = 500
    height = 300
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置

    img_open = Image.open('5.png')
    img_png = ImageTk.PhotoImage(img_open)
    label = Label(
        master=win,  # 父容器
        text='標籤',  # 文字
        # bg='pink',  # 背景顏色
        fg='red',  # 文字顏色
        activebackground='pink',  # 狀態為active時的背景顏色
        activeforeground='blue',  # 狀態為active的文字顏色
        relief='flat',  # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。預設為 FLAT。
        bd=3,  # 邊框的大小
        height=64,  # 高度
        width=64,  # 寬度
        padx=1,  # 內間距,字型與邊框的X距離
        pady=1,  # 內間距,字型與邊框的Y距離
        state='normal',  # 設定狀態 normal、active、 disabled 預設 normal
        cursor='arrow',  # 滑鼠移動時樣式 arrow, circle, cross, plus...
        font=('黑體', 20),  # 字型
        image=img_png,  # 圖片
    )
    label.pack()
    win.mainloop()