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

 

posted @ 2020-11-29 19:29  南风丶轻语  阅读(2133)  评论(0编辑  收藏  举报