当前位置:   article > 正文

Python 按钮(BUTTON)样式属性说明_python tk button属性

python tk button属性

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

使用语法:

widget = Button( master, parameter=value, ... )

    master:按钮控件的父容器
    parameter:按钮的参数
    value:参数对应的值

各参数之间以逗号分隔。

参数说明:
 

 

 

 代码示例:

  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()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/302533
推荐阅读
相关标签
  

闽ICP备14008679号