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()

 


免責聲明!

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



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