Python tkinter(一) 按钮(Button)组件的属性说明及示例

Python tkinter 按钮组件用于tkinter GUI里添加按钮,按钮可以添加文本和图像。当按钮按下时,可以执行指定的函数。

使用语法:

widget = Button( master, parameter=value, ... )
  • master:按钮控件的父容器
  • parameter:按钮的参数
  • value:参数对应的值

各参数之间以逗号分隔。

参数说明:

state按钮状态选项,状态有DISABLED/NORMAL/ACTIVE
activebackground当鼠标放上去时,按钮的背景色
activeforeground当鼠标放上去时,按钮的前景色
bd按钮边框的大小,默认为 2 个像素
bg按钮的背景色
fg按钮的前景色(按钮文本的颜色)
font文本字体,文字字号,文字字形。字形有overstrike/italic/bold/underline
height按钮的高度,如未设置此项,其大小以适应按钮的内容(文本或图片的大小)
width按钮的宽度,如未设置此项,其大小以适应按钮的内容(文本或图片的大小)
image按钮上要显示的图片,图片必须以变量的形式赋值给image,图片必须是gif格式。
justify显示多行文本的时候,设置不同行之间的对齐方式,可选项包括LEFT, RIGHT, CENTER
padx按钮在x轴方向上的内边距(padding),是指按钮的内容与按钮边缘的距离
pady按钮在y轴方向上的内边距(padding)
relief边框样式,设置控件显示效果,可选的有:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。
wraplength限制按钮每行显示的字符的数量,超出限制数量后则换行显示
underline下划线。默认按钮上的文本都不带下划线。取值就是带下划线的字符串索引,为 0 时,第一个字符带下划线,为 1 时,第两个字符带下划线,以此类推
text按钮的文本内容
command按钮关联的函数,当按钮被点击时,执行该函数

显示详细信息

代码示例:

 
  1. # -*- coding:utf-8 -*-

  2. from tkinter import *

  3. class buttons:

  4. def __init__(self):

  5. root = Tk()

  6. root.title("按钮") # 设置窗口标题

  7. root.geometry("600x600") # 设置窗口大小 注意:是x 不是*

  8. '''按钮样式'''

  9. # 按钮文字切换

  10. self.btsd = Label(root, text='按钮文字切换:')

  11. self.bts = Button(root, text='按钮开始', command=self.Button_text_switch)

  12. # 按钮状态

  13. self.button_state = Label(root, text='按钮状态:')

  14. self.disabled_state = Button(root, text='禁用状态')

  15. self.disabled_state.config(state=DISABLED)

  16. self.usual_status = Button(root, text='普通状态')

  17. self.usual_status.config(state=NORMAL)

  18. self.active = Button(root, text='活跃状态')

  19. self.active.config(state=ACTIVE)

  20. # 鼠标点击到按钮后改变颜色,activebackground='背景色',activeforeground='前景色'

  21. self.mouse_click_color = Label(root, text='鼠标点击颜色:')

  22. self.click_background_colour = Button(root, text='背景色', activebackground='blue')

  23. self.click_foreground_colour = Button(root, text='前景色', activeforeground='blue')

  24. # 按钮边框大小,bd='边框大小'

  25. self.button_border_size = Label(root, text='按钮边框大小:')

  26. self.border = Button(root, text='按钮边框', bd=5)

  27. # 按钮颜色,bg='背景色', fg='前景色'

  28. self.the_button_color = Label(root, text='按钮颜色:')

  29. self.button_background_colour = Button(root, text='背景色', bg='blue')

  30. self.button_foreground_colour = Button(root, text='前景色', fg='blue')

  31. # 按钮字体格式, font=('字体', 字体大小, 'bold/italic/underline/overstrike')

  32. self.button_font_format = Label(root, text='按钮字体格式:')

  33. self.button_face1 = Button(root, text='软体雅黑/12/重打印', font=('软体雅黑', 10, 'overstrike'))

  34. self.button_face2 = Button(root, text='宋体/12/斜体', font=('宋体', 10, 'italic'))

  35. self.button_face3 = Button(root, text='黑体/12/加粗', font=('黑体', 10, 'bold'))

  36. self.button_face4 = Button(root, text='楷体/12/下划线', font=('楷体', 10, 'underline'))

  37. # 按钮高度,height='高度'

  38. self.button_border_xy = Label(root, text='按钮边xy:')

  39. self.button_height = Button(root, text='按钮高度', height=2)

  40. self.button_width = Button(root, text='按钮宽度', width=16)

  41. # 按钮图片设置,image=图片变量。图片必须以变量的形式赋值给image,图片必须是gif格式。

  42. self.button_image_settings = Label(root, text='按钮图片设置:')

  43. gif = PhotoImage(file="1.gif")

  44. self.button_image = Button(root, image=gif)

  45. # 按钮文字对齐方式,可选项包括LEFT, RIGHT, CENTER

  46. self.text_alignment = Label(root, text='文字对齐方式:')

  47. self.text_left = Button(root, text='左对齐\n文字左侧对齐', justify=LEFT)

  48. self.text_center = Button(root, text='居中对齐\n文字居中对齐', justify=CENTER)

  49. self.text_tight = Button(root, text='右对齐\n文字右侧对齐', justify=RIGHT)

  50. # 按钮文字与边框之间的间距,padx='x轴方向间距大小',pady='y轴间距大小'

  51. self.text_border_spacing = Label(root, text='文字边框间距:')

  52. self.button_padx = Button(root, text='x轴间距', padx=0)

  53. self.button_pady = Button(root, text='y轴间距', pady=10)

  54. # 框样式,设置控件3D效果,可选的有:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。

  55. self.box_style = Label(root, text='按钮框样式:')

  56. self.button_relief1 = Button(root, text='边框平坦', relief=FLAT)

  57. self.button_relief2 = Button(root, text='边框凹陷', relief=SUNKEN)

  58. self.button_relief3 = Button(root, text='边框凸起', relief=RAISED)

  59. self.button_relief4 = Button(root, text='边框压线', relief=GROOVE)

  60. self.button_relief5 = Button(root, text='边框脊线', relief=RIDGE)

  61. # 按钮达到限制字符后换行显示

  62. self.Line_shows_state = Label(root, text='文字换行显示:')

  63. self.selfLine_shows = Button(root, text='1234567890', wraplength=30)

  64. # 下划线。取值就是带下划线的字符串索引,为 0 时,第一个字符带下划线,为 1 时,第两个字符带下划线,以此类推

  65. self.underline_state = Label(root, text='文字标下划线:')

  66. self.underline = Button(root, text='12345', underline=2)

  67. '''grid布局'''

  68. self.btsd.grid(row=1, column=1, sticky='E')

  69. self.bts.grid(row=1, column=2, sticky='NW')

  70. self.button_state.grid(row=2, column=1, sticky='E')

  71. self.disabled_state.grid(row=2, column=2, sticky='NW')

  72. self.usual_status.grid(row=2, column=3, sticky='NW')

  73. self.active.grid(row=2, column=4, sticky='NW')

  74. self.mouse_click_color.grid(row=3, column=1, sticky='E')

  75. self.click_background_colour.grid(row=3, column=2, sticky='NW')

  76. self.click_foreground_colour.grid(row=3, column=3, sticky='NW')

  77. self.button_border_size.grid(row=4, column=1, sticky='E')

  78. self.border.grid(row=4, column=2, columnspan=3, sticky='NW')

  79. self.the_button_color.grid(row=5, column=1, sticky='E')

  80. self.button_background_colour.grid(row=5, column=2, sticky='NW')

  81. self.button_foreground_colour.grid(row=5, column=3, sticky='NW')

  82. self.button_font_format.grid(row=6, column=1, sticky='E')

  83. self.button_face1.grid(row=6, column=2, columnspan=2, sticky='NW')

  84. self.button_face2.grid(row=6, column=4, columnspan=2, sticky='NW')

  85. self.button_face3.grid(row=6, column=6, columnspan=2, sticky='NW')

  86. self.button_face4.grid(row=6, column=8, columnspan=2, sticky='NW')

  87. self.button_border_xy.grid(row=7, column=1, sticky='E')

  88. self.button_height.grid(row=7, column=2, sticky='NW')

  89. self.button_width.grid(row=7, column=3, columnspan=2, sticky='NW')

  90. self.button_image_settings.grid(row=8, column=1, sticky='E')

  91. self.button_image.grid(row=8, column=2, columnspan=3, sticky='NW')

  92. self.text_alignment.grid(row=9, column=1, sticky='E')

  93. self.text_left.grid(row=9, column=2, columnspan=2, sticky='NW')

  94. self.text_center.grid(row=9, column=4, columnspan=2, sticky='NW')

  95. self.text_tight.grid(row=9, column=6, columnspan=2, sticky='NW')

  96. self.text_border_spacing.grid(row=10, column=1, sticky='E')

  97. self.button_padx.grid(row=10, column=2, sticky='NW')

  98. self.button_pady.grid(row=10, column=3, sticky='NW')

  99. self.box_style.grid(row=11, column=1, sticky='E')

  100. self.button_relief1.grid(row=11, column=2, sticky='NW')

  101. self.button_relief2.grid(row=11, column=3, sticky='NW')

  102. self.button_relief3.grid(row=11, column=4, sticky='NW')

  103. self.button_relief4.grid(row=11, column=5, sticky='NW')

  104. self.button_relief5.grid(row=11, column=6, sticky='NW')

  105. self.Line_shows_state.grid(row=12, column=1, sticky='E')

  106. self.selfLine_shows.grid(row=12, column=2, sticky='NW')

  107. self.underline_state.grid(row=13, column=1, sticky='E')

  108. self.underline.grid(row=13, column=2, sticky='NW')

  109. root.mainloop()

  110. # 按钮开关设置

  111. def Button_text_switch(self):

  112. if self.bts['text'] == '按钮开始': # 如果文字是开始,则改为关闭

  113. self.bts['text'] = '按钮关闭'

  114. print('按钮开始')

  115. else: # 如果不是开始,则改为开始

  116. self.bts['text'] = '按钮开始'

  117. print('按钮关闭')

  118. if __name__ == '__main__':

  119. buttons()

效果展示:

相关文档推荐:

python tkinter(二) 下拉框(combobox)组件的属性说明及示例

Python tkinter(三) 单选框(Radiobutton)组件的属性说明及示例

Python tkinter(四) 文本框(Text)组件的属性说明及示例

Python tkinter(五) 文本框(Entry)组件的属性说明及示例

Python tkinter(六) 标签(Label)组件的属性说明及示例

Python tkinter(一) 按钮(Button)组件的属性说明及示例_tkinter button更改属性-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值