当前位置:   article > 正文

Python如何使用Tkinter中的RadioButton单选按钮?代码示例_python tk radio button

python tk radio button

Radiobutton是一个标准的Tkinter小部件,用于实现许多选项中的一个。单选按钮可以包含文本或图像,您可以将Python函数或方法与每个按钮关联起来。当按下按钮时,Tkinter会自动调用该函数或方法。

语法如下:

button = Radiobutton(master, text= " Name on button ", variable = " shared variable ", value = " values of each button ", options = values,…)

shared variable =所有单选按钮共享的Tkinter变量

value =每个单选按钮应该有不同的值,否则超过1个单选按钮将被选中。

代码1:

单选按钮, 但不是按钮形式, 而是按钮盒。为了显示按钮框指示器/指示器选项应设置为0。

  1. # Importing Tkinter module
  2. from tkinter import *
  3. # from tkinter.ttk import *
  4. # Creating master Tkinter window
  5. master = Tk()
  6. master.geometry( "175x175" )
  7. # Tkinter string variable
  8. # able to store any string value
  9. v = StringVar(master, "1" )
  10. # Dictionary to create multiple buttons
  11. values = { "RadioButton 1" : "1" , "RadioButton 2" : "2" , "RadioButton 3" : "3" , "RadioButton 4" : "4" , "RadioButton 5" : "5" }
  12. # Loop is used to create multiple Radiobuttons
  13. # rather than creating each button separately
  14. for (text, value) in values.items():
  15. Radiobutton(master, text = text, variable = v, value = value, indicator = 0 , background = "light blue" ).pack(fill = X, ipady = 5 )
  16. # Infinite loop can be terminated by
  17. # keyboard or mouse interrupt
  18. # or by any predefined function (destroy())
  19. mainloop()

输出如下:

这些按钮框的背景是浅蓝色。具有白色背景以及下沉的按钮框被选中。

代码2:

将按钮框更改为标准单选按钮。对于此删除指标选项。

  1. # Importing Tkinter module
  2. from tkinter import * from tkinter.ttk import *
  3. # Creating master Tkinter window
  4. master = Tk()
  5. master.geometry( "175x175" )
  6. # Tkinter string variable
  7. # able to store any string value
  8. v = StringVar(master, "1" )
  9. # Dictionary to create multiple buttons
  10. values = { "RadioButton 1" : "1" , "RadioButton 2" : "2" , "RadioButton 3" : "3" , "RadioButton 4" : "4" , "RadioButton 5" : "5" }
  11. # Loop is used to create multiple Radiobuttons
  12. # rather than creating each button separately
  13. for (text, value) in values.items():
  14. Radiobutton(master, text = text, variable = v, value = value).pack(side = TOP, ipady = 5 )
  15. # Infinite loop can be terminated by
  16. # keyboard or mouse interrupt
  17. # or by any predefined function (destroy())
  18. mainloop()

输出如下:

这些单选按钮是使用tkinter创建的。这就是为什么背景选项是不可用的,但是我们可以使用style类来做样式。

代码3:使用添加样式到单选按钮风格班.

  1. # Importing Tkinter module
  2. from tkinter import * from tkinter.ttk import *
  3. # Creating master Tkinter window
  4. master = Tk()
  5. master.geometry( '175x175' )
  6. # Tkinter string variable
  7. # able to store any string value
  8. v = StringVar(master, "1" )
  9. # Style class to add style to Radiobutton
  10. # it can be used to style any ttk widget
  11. style = Style(master)
  12. style.configure( "TRadiobutton" , background = "light green" , foreground = "red" , font = ( "arial" , 10 , "bold" ))
  13. # Dictionary to create multiple buttons
  14. values = { "RadioButton 1" : "1" , "RadioButton 2" : "2" , "RadioButton 3" : "3" , "RadioButton 4" : "4" , "RadioButton 5" : "5" }
  15. # Loop is used to create multiple Radiobuttons
  16. # rather than creating each button separately
  17. for (text, value) in values.items():
  18. Radiobutton(master, text = text, variable = v, value = value).pack(side = TOP, ipady = 5 )
  19. # Infinite loop can be terminated by
  20. # keyboard or mouse interrupt
  21. # or by any predefined function (destroy())
  22. mainloop()

输出如下:

你可以观察到字体样式改变了,背景和前景色也改变了。这里,tradobutton是在style类中使用的,它会自动对所有可用的单选按钮应用样式。

更多内容可参考:lsbin - IT开发技术

更多关于Python Tkinter相关的内容:

Python Tkinter创建按钮

Python Tkinter中的Combobox

Python Tkinter滚动条

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号