Tkinter Radiobutton

本文详细介绍了Python GUI库Tkinter中的Radiobutton组件,包括其功能、语法、参数、方法及示例代码,帮助读者理解如何在GUI应用中实现单选功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python GUI - Tkinter Radiobutton: 这个小部件实现了多项选择按钮,这是一种方式向用户提供许多可能的选择,让用户选择只是其中之一.
 
这个小部件实现了多项选择按钮,这是一种方式向用户提供许多可能的选择,让用户选择只是其中之一.

为了实现这个功能,每个单选按钮组必须关联到相同的变量,每一个按钮必须象征着一个单一的值。你可以使用Tab键从一个radionbutton切换到另一个.

语法:

这里是一个简单的语法来创建这个widget:

w = Radiobutton ( master, option, ...  )

参数:

  • master: 这代表了父窗口.

  • options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.

OptionDescription
activebackgroundThe background color when the mouse is over the radiobutton.
activeforegroundThe foreground color when the mouse is over the radiobutton.
anchorIf the widget inhabits a space larger than it needs, this option specifies where the radiobutton will sit in that space. The default is anchor=CENTER.
bgThe normal background color behind the indicator and label.
bitmapTo display a monochrome image on a radiobutton, set this option to a bitmap.
borderwidthThe size of the border around the indicator part itself. Default is 2 pixels.
commandA procedure to be called every time the user changes the state of this radiobutton.
cursorIf you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the radiobutton.
fontThe font used for the text.
fgThe color used to render the text.
heightThe number of lines (not pixels) of text on the radiobutton. Default is 1.
highlightbackgroundThe color of the focus highlight when the radiobutton does not have focus.
highlightcolorThe color of the focus highlight when the radiobutton has the focus.
imageTo display a graphic image instead of text for this radiobutton, set this option to an image object.
justifyIf the text contains multiple lines, this option controls how the text is justified: CENTER (the default), LEFT, or RIGHT.
padxHow much space to leave to the left and right of the radiobutton and text. Default is 1.
padyHow much space to leave above and below the radiobutton and text. Default is 1.
reliefSpecifies the appearance of a decorative border around the label. The default is FLAT; for other values.
selectcolorThe color of the radiobutton when it is set. Default is red.
selectimageIf you are using the image option to display a graphic instead of text when the radiobutton is cleared, you can set the selectimage option to a different image that will be displayed when the radiobutton is set.
stateThe default is state=NORMAL, but you can set state=DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the radiobutton, the state is ACTIVE.
textThe label displayed next to the radiobutton. Use newlines ("\n") to display multiple lines of text.
textvariableTo slave the text displayed in a label widget to a control variable of class StringVar, set this option to that variable.
underlineYou can display an underline (_) below the nth letter of the text, counting from 0, by setting this option to n. The default is underline=-1, which means no underlining.
valueWhen a radiobutton is turned on by the user, its control variable is set to its current value option. If the control variable is anIntVar, give each radiobutton in the group a different integer value option. If the control variable is a StringVar, give each radiobutton a different string value option.
variableThe control variable that this radiobutton shares with the other radiobuttons in the group. This can be either an IntVar or a StringVar.
widthWidth of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
wraplengthYou can limit the number of characters in each line by setting this option to the desired number. The default value, 0, means that lines will be broken only at newlines.

方法:

MethodsDescription
deselect()Clears (turns off) the radiobutton.
flash()Flashes the radiobutton a few times between its active and normal colors, but leaves it the way it started.
invoke()You can call this method to get the same actions that would occur if the user clicked on the radiobutton to change its state.
select()Sets (turns on) the radiobutton.

例子:

自行尝试下面的例子:

from Tkinter import *

def sel():
   selection = "You selected the option " + str(var.get())
   label.config(text = selection)

root = Tk()
var = IntVar()
R1 = Radiobutton(root, text="Option 1", variable=var, value=1,
                  command=sel)
R1.pack( anchor = W )

R2 = Radiobutton(root, text="Option 2", variable=var, value=2,
                  command=sel)
R2.pack( anchor = W )

R3 = Radiobutton(root, text="Option 3", variable=var, value=3,
                  command=sel)
R3.pack( anchor = W)

label = Label(root)
label.pack()
root.mainloop()

这将产生以下结果:

 

转载于:https://www.cnblogs.com/tkinter/p/5628843.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值