Python中按鈕(BUTTON)樣式屬性及說明
更新時間:2025年01月23日 10:08:35 作者:SAPmatinal
文章介紹了Python中tkinter庫中的Button組件,用于在GUI中添加按鈕,按鈕可以包含文本或圖像,并且可以通過點擊執(zhí)行特定函數(shù),文章詳細(xì)說明了Button組件的構(gòu)造語法和常用參數(shù),并提供了一個代碼示例
Python按鈕(BUTTON)樣式屬性
Python tkinter 按鈕組件用于tkinter GUI里添加按鈕,按鈕可以添加文本和圖像。
當(dāng)按鈕按下時,可以執(zhí)行指定的函數(shù)。
使用語法
widget = Button( master, parameter=value, ... )
master:按鈕控件的父容器parameter:按鈕的參數(shù)value:參數(shù)對應(yīng)的值
各參數(shù)之間以逗號分隔。
參數(shù)說明



代碼示例
# -*- coding:utf-8 -*-
from tkinter import *
class buttons:
def __init__(self):
root = Tk()
root.title("按鈕") # 設(shè)置窗口標(biāo)題
root.geometry("600x600") # 設(shè)置窗口大小 注意:是x 不是*
'''按鈕樣式'''
# 按鈕文字切換
self.btsd = Label(root, text='按鈕文字切換:')
self.bts = Button(root, text='按鈕開始', command=self.Button_text_switch)
# 按鈕狀態(tài)
self.button_state = Label(root, text='按鈕狀態(tài):')
self.disabled_state = Button(root, text='禁用狀態(tài)')
self.disabled_state.config(state=DISABLED)
self.usual_status = Button(root, text='普通狀態(tài)')
self.usual_status.config(state=NORMAL)
self.active = Button(root, text='活躍狀態(tài)')
self.active.config(state=ACTIVE)
# 鼠標(biāo)點擊到按鈕后改變顏色,activebackground='背景色',activeforeground='前景色'
self.mouse_click_color = Label(root, text='鼠標(biāo)點擊顏色:')
self.click_background_colour = Button(root, text='背景色', activebackground='blue')
self.click_foreground_colour = Button(root, text='前景色', activeforeground='blue')
# 按鈕邊框大小,bd='邊框大小'
self.button_border_size = Label(root, text='按鈕邊框大?。?)
self.border = Button(root, text='按鈕邊框', bd=5)
# 按鈕顏色,bg='背景色', fg='前景色'
self.the_button_color = Label(root, text='按鈕顏色:')
self.button_background_colour = Button(root, text='背景色', bg='blue')
self.button_foreground_colour = Button(root, text='前景色', fg='blue')
# 按鈕字體格式, font=('字體', 字體大小, 'bold/italic/underline/overstrike')
self.button_font_format = Label(root, text='按鈕字體格式:')
self.button_face1 = Button(root, text='軟體雅黑/12/重打印', font=('軟體雅黑', 10, 'overstrike'))
self.button_face2 = Button(root, text='宋體/12/斜體', font=('宋體', 10, 'italic'))
self.button_face3 = Button(root, text='黑體/12/加粗', font=('黑體', 10, 'bold'))
self.button_face4 = Button(root, text='楷體/12/下劃線', font=('楷體', 10, 'underline'))
# 按鈕高度,height='高度'
self.button_border_xy = Label(root, text='按鈕邊xy:')
self.button_height = Button(root, text='按鈕高度', height=2)
self.button_width = Button(root, text='按鈕寬度', width=16)
# 按鈕圖片設(shè)置,image=圖片變量。圖片必須以變量的形式賦值給image,圖片必須是gif格式。
self.button_image_settings = Label(root, text='按鈕圖片設(shè)置:')
gif = PhotoImage(file="1.gif")
self.button_image = Button(root, image=gif)
# 按鈕文字對齊方式,可選項包括LEFT, RIGHT, CENTER
self.text_alignment = Label(root, text='文字對齊方式:')
self.text_left = Button(root, text='左對齊\n文字左側(cè)對齊', justify=LEFT)
self.text_center = Button(root, text='居中對齊\n文字居中對齊', justify=CENTER)
self.text_tight = Button(root, text='右對齊\n文字右側(cè)對齊', justify=RIGHT)
# 按鈕文字與邊框之間的間距,padx='x軸方向間距大小',pady='y軸間距大小'
self.text_border_spacing = Label(root, text='文字邊框間距:')
self.button_padx = Button(root, text='x軸間距', padx=0)
self.button_pady = Button(root, text='y軸間距', pady=10)
# 框樣式,設(shè)置控件3D效果,可選的有:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。
self.box_style = Label(root, text='按鈕框樣式:')
self.button_relief1 = Button(root, text='邊框平坦', relief=FLAT)
self.button_relief2 = Button(root, text='邊框凹陷', relief=SUNKEN)
self.button_relief3 = Button(root, text='邊框凸起', relief=RAISED)
self.button_relief4 = Button(root, text='邊框壓線', relief=GROOVE)
self.button_relief5 = Button(root, text='邊框脊線', relief=RIDGE)
# 按鈕達(dá)到限制字符后換行顯示
self.Line_shows_state = Label(root, text='文字換行顯示:')
self.selfLine_shows = Button(root, text='1234567890', wraplength=30)
# 下劃線。取值就是帶下劃線的字符串索引,為 0 時,第一個字符帶下劃線,為 1 時,第兩個字符帶下劃線,以此類推
self.underline_state = Label(root, text='文字標(biāo)下劃線:')
self.underline = Button(root, text='12345', underline=2)
'''grid布局'''
self.btsd.grid(row=1, column=1, sticky='E')
self.bts.grid(row=1, column=2, sticky='NW')
self.button_state.grid(row=2, column=1, sticky='E')
self.disabled_state.grid(row=2, column=2, sticky='NW')
self.usual_status.grid(row=2, column=3, sticky='NW')
self.active.grid(row=2, column=4, sticky='NW')
self.mouse_click_color.grid(row=3, column=1, sticky='E')
self.click_background_colour.grid(row=3, column=2, sticky='NW')
self.click_foreground_colour.grid(row=3, column=3, sticky='NW')
self.button_border_size.grid(row=4, column=1, sticky='E')
self.border.grid(row=4, column=2, columnspan=3, sticky='NW')
self.the_button_color.grid(row=5, column=1, sticky='E')
self.button_background_colour.grid(row=5, column=2, sticky='NW')
self.button_foreground_colour.grid(row=5, column=3, sticky='NW')
self.button_font_format.grid(row=6, column=1, sticky='E')
self.button_face1.grid(row=6, column=2, columnspan=2, sticky='NW')
self.button_face2.grid(row=6, column=4, columnspan=2, sticky='NW')
self.button_face3.grid(row=6, column=6, columnspan=2, sticky='NW')
self.button_face4.grid(row=6, column=8, columnspan=2, sticky='NW')
self.button_border_xy.grid(row=7, column=1, sticky='E')
self.button_height.grid(row=7, column=2, sticky='NW')
self.button_width.grid(row=7, column=3, columnspan=2, sticky='NW')
self.button_image_settings.grid(row=8, column=1, sticky='E')
self.button_image.grid(row=8, column=2, columnspan=3, sticky='NW')
self.text_alignment.grid(row=9, column=1, sticky='E')
self.text_left.grid(row=9, column=2, columnspan=2, sticky='NW')
self.text_center.grid(row=9, column=4, columnspan=2, sticky='NW')
self.text_tight.grid(row=9, column=6, columnspan=2, sticky='NW')
self.text_border_spacing.grid(row=10, column=1, sticky='E')
self.button_padx.grid(row=10, column=2, sticky='NW')
self.button_pady.grid(row=10, column=3, sticky='NW')
self.box_style.grid(row=11, column=1, sticky='E')
self.button_relief1.grid(row=11, column=2, sticky='NW')
self.button_relief2.grid(row=11, column=3, sticky='NW')
self.button_relief3.grid(row=11, column=4, sticky='NW')
self.button_relief4.grid(row=11, column=5, sticky='NW')
self.button_relief5.grid(row=11, column=6, sticky='NW')
self.Line_shows_state.grid(row=12, column=1, sticky='E')
self.selfLine_shows.grid(row=12, column=2, sticky='NW')
self.underline_state.grid(row=13, column=1, sticky='E')
self.underline.grid(row=13, column=2, sticky='NW')
root.mainloop()
# 按鈕開關(guān)設(shè)置
def Button_text_switch(self):
if self.bts['text'] == '按鈕開始': # 如果文字是開始,則改為關(guān)閉
self.bts['text'] = '按鈕關(guān)閉'
print('按鈕開始')
else: # 如果不是開始,則改為開始
self.bts['text'] = '按鈕開始'
print('按鈕關(guān)閉')
if __name__ == '__main__':
buttons()
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python刪除PDF中多余或空白頁面的實現(xiàn)步驟
在處理 PDF 文件時,常常會遇到一些多余或空白的頁面,這些頁面不僅占據(jù)存儲空間,還會影響文檔的整潔性和可讀性,這篇文章將探討如何使用 Python刪除PDF中多余或空白的頁面,需要的朋友可以參考下2025-05-05
Python代碼實現(xiàn)復(fù)制PowerPoint幻燈片
在處理演示文稿時,復(fù)制幻燈片是一項非常實用的功能,本文將詳細(xì)介紹如何使用 Spire.Presentation for Python 庫復(fù)制 PowerPoint 幻燈片,,幫助你構(gòu)建完整的幻燈片管理解決方案2026-06-06
Python處理圖像并生成JSONL元數(shù)據(jù)文件
JSONL是一種輕量級的數(shù)據(jù)序列化格式,由一系列獨立的?JSON?對象組成,本文主要為大家介紹了Python如何處理圖像并生成JSONL元數(shù)據(jù)文件,感興趣的可以了解下2025-05-05
探索Python?Slice函數(shù)靈活而強大的序列切片技術(shù)
Python中的Slice函數(shù)是一種強大且靈活的序列切片技術(shù),用于從字符串、列表、元組等序列類型中提取子集,本文將深入研究Slice函數(shù)的功能和用法,提供詳細(xì)的示例代碼和解釋,幫助讀者更全面地了解和應(yīng)用這一功能2024-01-01

