Python tkinter之Button

1、Button的基本属性

# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *


def event():
    print('点击事件')


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))  # 大小以及位置

    button = Button(
            master=win,  # 父容器
            text='标签',  # 文本
            bg='yellow',  # 背景颜色
            fg='red',  # 文本颜色
            activebackground='pink',  # 状态为active时的背景颜色
            activeforeground='blue',  # 状态为active的文字颜色
            relief='raised',  # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。默认为 raised。
            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),  # 字体
            command=event,  # 点击事件
            )
    button.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 *


def event():
    print('点击事件')


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):
        button = Button(
                master=win,  # 父容器
                text=value,  # 文本
                bg='yellow',  # 背景颜色
                fg='red',  # 文本颜色
                activebackground='pink',  # 状态为active时的背景颜色
                activeforeground='blue',  # 状态为active的文字颜色
                relief=value,  # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。
                bd=5,  # 边框的大小
                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),  # 字体
                command=event,  # 点击事件
                )
        button.grid(row=0, column=index, padx=10, pady=10)

    values = ['normal', 'active', 'disabled']
    for index, value in enumerate(values):
        button = Button(
                master=win,  # 父容器
                text=value,  # 文本
                bg='yellow',  # 背景颜色
                fg='red',  # 文本颜色
                activebackground='pink',  # 状态为active时的背景颜色
                activeforeground='blue',  # 状态为active的文字颜色
                relief='raised',  # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。
                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),  # 字体
                command=event,  # 点击事件
                )
        button.grid(row=1, column=index, padx=10, pady=10)

    win.mainloop()

 3、显示图片的Button

tkinter只支持gif图片,如果使用其他格式图片,需要使用PIL模块

# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *

from PIL import Image
from PIL import ImageTk


def event():
    print('点击事件')


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('../img/19.png')
    img_png = ImageTk.PhotoImage(img_open)
    button = Button(
            master=win,  # 父容器
            text='标签',  # 文本
            bg='pink',  # 背景颜色
            fg='red',  # 文本颜色
            activebackground='pink',  # 状态为active时的背景颜色
            activeforeground='blue',  # 状态为active的文字颜色
            relief='groove',  # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。
            bd=3,  # 边框的大小
            height=64,  # 高度
            width=64,  # 宽度
            padx=1,  # 内间距,字体与边框的X距离
            pady=1,  # 内间距,字体与边框的Y距离
            state='normal',  # 设置状态 normal、active、 disabled
            cursor='arrow',  # 鼠标移动时样式 arrow, circle, cross, plus...
            font=('黑体', 20),  # 字体
            image=img_png,  # 图片
            command=event,  # 点击事件
            )
    button.pack()
    win.mainloop()

 

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