wxPython - 通过两步创建创建单选框
在本文中,我们将学习在框架中创建一个单选框。单选框项目用于选择多个互斥选项之一。它显示为带有标签的按钮的垂直列或水平行。
为了创建单选框,我们将使用 wxPython 的 wx.RadioBox 类中的 Create()函数。 Create()函数将单选框的不同属性作为参数。
Syntax:
wx.RadioBox.Create( parent, id=ID_ANY, label=””, pos=DefaultPosition,
size=DefaultSize, choices=[], majorDimension=0, style=RA_SPECIFY_COLS,
validator=DefaultValidator, name=RadioBoxNameStr )
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. |
choices | list of strings | Window style. |
majorDimension | int | Specifies the maximum number of rows (if style contains RA_SPECIFY_ROWS ) or columns (if style contains RA_SPECIFY_COLS ) for a two-dimensional radiobox. The default value of 0 means to use the number of items, i.e. number of elements in choices. |
style | long | An array of choices with which to initialize the radiobox. |
validator | wx.Validator | Window validator. |
name | string | Window name. |
代码示例:
import wx
class FrameUI(wx.Frame):
def __init__(self, parent, title):
super(FrameUI, self).__init__(parent, title = title, size =(300, 200))
# function for in-frame components
self.InitUI()
def InitUI(self):
# parent panel for radio box
pnl = wx.Panel(self)
# list of choices
lblList = ['Value X', 'Value Y', 'Value Z']
# create radio box
self.rbox = wx.RadioBox()
# create with two step creation using Create() function
self.rbox.Create(pnl, label ='RadioBox', pos =(80, 10), choices = lblList,
majorDimension = 1, style = wx.RA_SPECIFY_ROWS)
# set frame in centre
self.Centre()
# set size of frame
self.SetSize((400, 250))
# show output frame
self.Show(True)
# wx App instance
ex = wx.App()
# Example instance
FrameUI(None, 'RadioButton and RadioBox')
ex.MainLoop()
输出窗口: