赞
踩
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。
- # Importing Tkinter module
- from tkinter import *
- # from tkinter.ttk import *
-
- # Creating master Tkinter window
- master = Tk()
- master.geometry( "175x175" )
-
- # Tkinter string variable
- # able to store any string value
- v = StringVar(master, "1" )
-
- # Dictionary to create multiple buttons
- values = { "RadioButton 1" : "1" , "RadioButton 2" : "2" , "RadioButton 3" : "3" , "RadioButton 4" : "4" , "RadioButton 5" : "5" }
-
- # Loop is used to create multiple Radiobuttons
- # rather than creating each button separately
- for (text, value) in values.items():
- Radiobutton(master, text = text, variable = v, value = value, indicator = 0 , background = "light blue" ).pack(fill = X, ipady = 5 )
-
- # Infinite loop can be terminated by
- # keyboard or mouse interrupt
- # or by any predefined function (destroy())
- mainloop()
输出如下:
这些按钮框的背景是浅蓝色。具有白色背景以及下沉的按钮框被选中。
代码2:
将按钮框更改为标准单选按钮。对于此删除指标选项。
- # Importing Tkinter module
- from tkinter import * from tkinter.ttk import *
-
- # Creating master Tkinter window
- master = Tk()
- master.geometry( "175x175" )
-
- # Tkinter string variable
- # able to store any string value
- v = StringVar(master, "1" )
-
- # Dictionary to create multiple buttons
- values = { "RadioButton 1" : "1" , "RadioButton 2" : "2" , "RadioButton 3" : "3" , "RadioButton 4" : "4" , "RadioButton 5" : "5" }
-
- # Loop is used to create multiple Radiobuttons
- # rather than creating each button separately
- for (text, value) in values.items():
- Radiobutton(master, text = text, variable = v, value = value).pack(side = TOP, ipady = 5 )
-
- # Infinite loop can be terminated by
- # keyboard or mouse interrupt
- # or by any predefined function (destroy())
- mainloop()
输出如下:
这些单选按钮是使用tkinter创建的。这就是为什么背景选项是不可用的,但是我们可以使用style类来做样式。
代码3:使用添加样式到单选按钮风格班.
- # Importing Tkinter module
- from tkinter import * from tkinter.ttk import *
-
- # Creating master Tkinter window
- master = Tk()
- master.geometry( '175x175' )
-
- # Tkinter string variable
- # able to store any string value
- v = StringVar(master, "1" )
-
- # Style class to add style to Radiobutton
- # it can be used to style any ttk widget
- style = Style(master)
- style.configure( "TRadiobutton" , background = "light green" , foreground = "red" , font = ( "arial" , 10 , "bold" ))
-
- # Dictionary to create multiple buttons
- values = { "RadioButton 1" : "1" , "RadioButton 2" : "2" , "RadioButton 3" : "3" , "RadioButton 4" : "4" , "RadioButton 5" : "5" }
-
- # Loop is used to create multiple Radiobuttons
- # rather than creating each button separately
- for (text, value) in values.items():
- Radiobutton(master, text = text, variable = v, value = value).pack(side = TOP, ipady = 5 )
-
- # Infinite loop can be terminated by
- # keyboard or mouse interrupt
- # or by any predefined function (destroy())
- mainloop()
输出如下:
你可以观察到字体样式改变了,背景和前景色也改变了。这里,tradobutton是在style类中使用的,它会自动对所有可用的单选按钮应用样式。
更多内容可参考:lsbin - IT开发技术
更多关于Python Tkinter相关的内容:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。