Python中按鈕(BUTTON)樣式屬性及說明
更新時(shí)間:2025年01月23日 10:08:35 作者:SAPmatinal
文章介紹了Python中tkinter庫中的Button組件,用于在GUI中添加按鈕,按鈕可以包含文本或圖像,并且可以通過點(diǎn)擊執(zhí)行特定函數(shù),文章詳細(xì)說明了Button組件的構(gòu)造語法和常用參數(shù),并提供了一個(gè)代碼示例
Python按鈕(BUTTON)樣式屬性
Python tkinter 按鈕組件用于tkinter GUI里添加按鈕,按鈕可以添加文本和圖像。
當(dāng)按鈕按下時(shí),可以執(zhí)行指定的函數(shù)。
使用語法
widget = Button( master, parameter=value, ... )
master:按鈕控件的父容器parameter:按鈕的參數(shù)value:參數(shù)對應(yīng)的值
各參數(shù)之間以逗號(hào)分隔。
參數(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)點(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)
# 按鈕文字對齊方式,可選項(xiàng)包括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 時(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中利用try..except來代替if..else的用法
今天小編就為大家分享一篇在python中利用try..except來代替if..else的用法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python生成器generator和yield關(guān)鍵字的使用
生成器是一種特殊的迭代器,可以通過列表推導(dǎo)式的修改或者使用yield關(guān)鍵字來創(chuàng)建,生成器函數(shù)能夠在迭代時(shí)動(dòng)態(tài)產(chǎn)生值,而不是一次性生成所有值,這有助于節(jié)省內(nèi)存,yield關(guān)鍵字使函數(shù)執(zhí)行暫停并保存當(dāng)前狀態(tài),下次調(diào)用時(shí)從停止處繼續(xù)執(zhí)行2024-09-09
Python把csv數(shù)據(jù)寫入list和字典類型的變量腳本方法
今天小編就為大家分享一篇Python把csv數(shù)據(jù)寫入list和字典類型的變量腳本方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Request的中斷和ErrorHandler實(shí)例解析
這篇文章主要介紹了Request的中斷和ErrorHandler實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法,結(jié)合具體實(shí)例形式分析了Python哈夫曼樹的原理、定義及簡單使用方法,需要的朋友可以參考下2018-04-04

