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

 


免責聲明!

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



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