Python中按鈕(BUTTON)樣式屬性及說(shuō)明
Python按鈕(BUTTON)樣式屬性
Python tkinter 按鈕組件用于tkinter GUI里添加按鈕,按鈕可以添加文本和圖像。
當(dāng)按鈕按下時(shí),可以執(zhí)行指定的函數(shù)。
使用語(yǔ)法
widget = Button( master, parameter=value, ... )
master:按鈕控件的父容器parameter:按鈕的參數(shù)value:參數(shù)對(duì)應(yīng)的值
各參數(shù)之間以逗號(hào)分隔。
參數(shù)說(shuō)明



代碼示例
# -*- 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)點(diǎn)擊到按鈕后改變顏色,activebackground='背景色',activeforeground='前景色'
self.mouse_click_color = Label(root, text='鼠標(biāo)點(diǎn)擊顏色:')
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)
# 按鈕文字對(duì)齊方式,可選項(xiàng)包括LEFT, RIGHT, CENTER
self.text_alignment = Label(root, text='文字對(duì)齊方式:')
self.text_left = Button(root, text='左對(duì)齊\n文字左側(cè)對(duì)齊', justify=LEFT)
self.text_center = Button(root, text='居中對(duì)齊\n文字居中對(duì)齊', justify=CENTER)
self.text_tight = Button(root, text='右對(duì)齊\n文字右側(cè)對(duì)齊', 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 時(shí),第一個(gè)字符帶下劃線,為 1 時(shí),第兩個(gè)字符帶下劃線,以此類推
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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python刪除PDF中多余或空白頁(yè)面的實(shí)現(xiàn)步驟
在處理 PDF 文件時(shí),常常會(huì)遇到一些多余或空白的頁(yè)面,這些頁(yè)面不僅占據(jù)存儲(chǔ)空間,還會(huì)影響文檔的整潔性和可讀性,這篇文章將探討如何使用 Python刪除PDF中多余或空白的頁(yè)面,需要的朋友可以參考下2025-05-05
Python代碼實(shí)現(xiàn)復(fù)制PowerPoint幻燈片
在處理演示文稿時(shí),復(fù)制幻燈片是一項(xiàng)非常實(shí)用的功能,本文將詳細(xì)介紹如何使用 Spire.Presentation for Python 庫(kù)復(fù)制 PowerPoint 幻燈片,,幫助你構(gòu)建完整的幻燈片管理解決方案2026-06-06
Python處理圖像并生成JSONL元數(shù)據(jù)文件
JSONL是一種輕量級(jí)的數(shù)據(jù)序列化格式,由一系列獨(dú)立的?JSON?對(duì)象組成,本文主要為大家介紹了Python如何處理圖像并生成JSONL元數(shù)據(jù)文件,感興趣的可以了解下2025-05-05
python批處理將圖片進(jìn)行放大實(shí)例代碼
最近處理一些規(guī)格不一的照片,需要修改成指定尺寸便于打印,下面這篇文章主要給大家介紹了關(guān)于python批處理將圖片進(jìn)行放大的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
探索Python?Slice函數(shù)靈活而強(qiáng)大的序列切片技術(shù)
Python中的Slice函數(shù)是一種強(qiáng)大且靈活的序列切片技術(shù),用于從字符串、列表、元組等序列類型中提取子集,本文將深入研究Slice函數(shù)的功能和用法,提供詳細(xì)的示例代碼和解釋,幫助讀者更全面地了解和應(yīng)用這一功能2024-01-01
python OpenCV 實(shí)現(xiàn)高斯濾波詳解
這篇文章主要介紹了Python+OpenCV 實(shí)現(xiàn)高斯濾波的過(guò)程,關(guān)于高斯濾波的解釋,它是一種線性平滑濾波,適用于消除高斯噪聲,具體實(shí)現(xiàn)過(guò)程跟隨小編一起看看吧2021-10-10

