Python中按鈕(BUTTON)樣式屬性及說明
更新時間:2025年01月23日 10:08:35 作者:SAPmatinal
文章介紹了Python中tkinter庫中的Button組件,用于在GUI中添加按鈕,按鈕可以包含文本或圖像,并且可以通過點擊執(zhí)行特定函數(shù),文章詳細說明了Button組件的構(gòu)造語法和常用參數(shù),并提供了一個代碼示例
Python按鈕(BUTTON)樣式屬性
Python tkinter 按鈕組件用于tkinter GUI里添加按鈕,按鈕可以添加文本和圖像。
當按鈕按下時,可以執(zhí)行指定的函數(shù)。
使用語法
widget = Button( master, parameter=value, ... )
master
:按鈕控件的父容器parameter
:按鈕的參數(shù)value
:參數(shù)對應的值
各參數(shù)之間以逗號分隔。
參數(shù)說明
代碼示例
# -*- coding:utf-8 -*- from tkinter import * class buttons: def __init__(self): root = Tk() root.title("按鈕") # 設置窗口標題 root.geometry("600x600") # 設置窗口大小 注意:是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) # 鼠標點擊到按鈕后改變顏色,activebackground='背景色',activeforeground='前景色' self.mouse_click_color = Label(root, text='鼠標點擊顏色:') 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) # 按鈕圖片設置,image=圖片變量。圖片必須以變量的形式賦值給image,圖片必須是gif格式。 self.button_image_settings = Label(root, text='按鈕圖片設置:') 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) # 框樣式,設置控件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) # 按鈕達到限制字符后換行顯示 self.Line_shows_state = Label(root, text='文字換行顯示:') self.selfLine_shows = Button(root, text='1234567890', wraplength=30) # 下劃線。取值就是帶下劃線的字符串索引,為 0 時,第一個字符帶下劃線,為 1 時,第兩個字符帶下劃線,以此類推 self.underline_state = Label(root, text='文字標下劃線:') 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() # 按鈕開關設置 def Button_text_switch(self): if self.bts['text'] == '按鈕開始': # 如果文字是開始,則改為關閉 self.bts['text'] = '按鈕關閉' print('按鈕開始') else: # 如果不是開始,則改為開始 self.bts['text'] = '按鈕開始' print('按鈕關閉') if __name__ == '__main__': buttons()
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在python中利用try..except來代替if..else的用法
今天小編就為大家分享一篇在python中利用try..except來代替if..else的用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python生成器generator和yield關鍵字的使用
生成器是一種特殊的迭代器,可以通過列表推導式的修改或者使用yield關鍵字來創(chuàng)建,生成器函數(shù)能夠在迭代時動態(tài)產(chǎn)生值,而不是一次性生成所有值,這有助于節(jié)省內(nèi)存,yield關鍵字使函數(shù)執(zhí)行暫停并保存當前狀態(tài),下次調(diào)用時從停止處繼續(xù)執(zhí)行2024-09-09Python把csv數(shù)據(jù)寫入list和字典類型的變量腳本方法
今天小編就為大家分享一篇Python把csv數(shù)據(jù)寫入list和字典類型的變量腳本方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法,結(jié)合具體實例形式分析了Python哈夫曼樹的原理、定義及簡單使用方法,需要的朋友可以參考下2018-04-04