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

本文介绍了如何使用Python的Tkinter库创建不同样式的单选按钮,并提供了代码示例,展示了如何通过改变配置选项来定制按钮外观。

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相关的内容:

Python Tkinter创建按钮

Python Tkinter中的Combobox

Python Tkinter滚动条

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值