使用 wxPython 在框架中创建 RadioButton
在本文中,我们将学习 wxPython 中的单选按钮。单选按钮项是通常表示几个互斥选项之一的按钮。
它在(通常)圆形按钮旁边有一个文本标签。
您可以通过为组中的第一个指定 RB_GROUP 来创建一组互斥的单选按钮。当创建另一个单选按钮组或没有更多单选按钮时,该组结束。
Syntax:
wx.RadioButton.RadioButton(parent, id = ID_ANY, label = “”, pos = DefaultPosition,
size = DefaultSize, style = 0, validator = DefaultValidator,
name = RadioButtonNameStr)
Parameters:
Parameter | Input Type | Description |
---|---|---|
parent | wx.Window | Parent window. Should not be None. |
id | wx.WindowID | Control identifier. A value of -1 denotes a default value. |
label | string | Text Label. |
pos | wx.Point | Window position. |
size | wx.Window | Window size. |
style | long | Window style. |
validator | wx.Validator | Window validator. |
name | string | Window name. |
代码示例:
# importing the module
import wx
# definition of the Example class
class Example(wx.Frame):
# instantiating the class
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
# method for creation of user interface
def InitUI(self):
# create parent panel for radio buttons
self.pnl = wx.Panel(self)
# create radio button using RadioButton() constructor
self.rb = wx.RadioButton(self.pnl, id = 1, label ="Radio", pos =(20, 20))
# definition of the main function
def main():
# creating an App object
app = wx.App()
# creating an Example object
ex = Example(None)
# showing the Example object
ex.Show()
# running the App object
app.MainLoop()
# driver code
if __name__ == '__main__':
main()
输出窗口: