单选按钮通常在一个组中,一个或多个不止一个,至少应该点击一个,但您只有一个按钮。在这种情况下通常使用的是复选框,CheckBox
。
在此示例中,它打印激活TextCtrl
a 时输入的文本:CheckBox
#!python
# -*- coding: utf-8 -*-
import wx
class MyFrame(wx.Frame):
def __init__(self, title):
super(MyFrame, self).__init__(None, title=title)
panel = wx.Panel(self)
self.check = wx.CheckBox(panel, label='confiurm?', pos =(10,70), size=(90,-1))
self.text = wx.TextCtrl(panel, pos=(100,70), size=(180,-1))
# disable the button until the user enters something
self.check.Disable()
self.Bind(wx.EVT_CHECKBOX, self.OnCheck, self.check)
self.Bind(wx.EVT_TEXT, self.OnTypeText, self.text)
self.Centre()
def OnTypeText(self, event):
'''
OnTypeText is called when the user types some string and
activate the check box if there is a string.
'''
if( len(self.text.GetValue()) > 0 ):
self.check.Enable()
else:
self.check.Disable()
def OnCheck(self, event):
'''
Print the user input if he clicks the checkbox.
'''
if( self.check.IsChecked() ):
print(self.text.GetValue())
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame('Example')
self.frame.Show()
return True
MyApp(False).MainLoop()
这是它的工作原理: