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

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滚动条

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python使用Tkinter库创建RadioButton单选按钮)可以实现用户在一组选项选择一个选项的功能。以下是一个使用Tkinter创建RadioButton示例代码: ```python import tkinter as tk from tkinter import messagebox def show_selected(): messagebox.showinfo("Selection", f"You selected {var.get()}") # 创建主窗口 root = tk.Tk() root.title("RadioButton Demo") # 定义变量用于存储选项的值 var = tk.StringVar() # 创建RadioButton,并将选项与变量关联 radio1 = tk.Radiobutton(root, text="Option 1", variable=var, value="Option 1") radio1.pack() radio2 = tk.Radiobutton(root, text="Option 2", variable=var, value="Option 2") radio2.pack() radio3 = tk.Radiobutton(root, text="Option 3", variable=var, value="Option 3") radio3.pack() # 创建按钮,用于显示当前选择的选项 button = tk.Button(root, text="Show Selected", command=show_selected) button.pack() # 进入主循环 root.mainloop() ``` 在这个例子,我们首先导入了`tkinter`库,并且从`tkinter`模块导入了`messagebox`用于显示消息框。 然后,我们创建了一个主窗口,并为其设置了标题。 接着,我们定义了一个变量`var`用于存储选项的值。然后创建了三个RadioButton,分别代表三个选项,然后将这三个选项分别与变量`var`关联起来。 最后,我们创建了一个按钮,当用户点击该按钮时,调用`show_selected`函数,该函数会弹出一个消息框,显示当前选择的选项。 最后调用`root.mainloop()`进入主循环,等待用户交互。 通过这个例子,你可以在Python使用Tkinter库创建RadioButton,并实现相关功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值