2012-12-05 147 views
0

我希望有一个放置在水平框内的textctrl。在wxpython默认情况下隐藏TextCtrl

self.myTextCtrl = wx.TextCtrl(面板,-1, “的Bleh”)

self.vbox.Add(self.myTextCtrl,比例= 1)

: 我使用此代码由该ATM

此代码在屏幕上打印我的标签。

但是,我有一个单选按钮之上(默认为false),当我将它设置为true时,我希望该框显示。 我试图调用 self.myTextCtrl.Hide()

(出现该隐藏在由无线电butto切换触发的事件)

但是这导致textctrl不能够在以后加载.. 。

Some1告诉我,它不得不与wxpython编译你的程序,因为你放弃了它,但是我无法在网上找到关于它的信息。

请帮忙。

回答

1

我掀起了一个快速而肮脏的例子。单选按钮一旦设置为True,就不能被“取消选中”,除非你在组中使用它们,所以我还包括一个使用CheckBox小部件的例子。我还添加了空白文本控件作为需要接受焦点而不用为其他小部件设置事件:

import wx 

######################################################################## 
class MyPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 

     txt = wx.TextCtrl(self) 
     radio1 = wx.RadioButton(self, -1, " Radio1 ") 
     radio1.Bind(wx.EVT_RADIOBUTTON, self.onRadioButton) 
     self.hiddenText = wx.TextCtrl(self) 
     self.hiddenText.Hide() 

     self.checkBtn = wx.CheckBox(self) 
     self.checkBtn.Bind(wx.EVT_CHECKBOX, self.onCheckBox) 
     self.hiddenText2 = wx.TextCtrl(self) 
     self.hiddenText2.Hide() 

     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(txt, 0, wx.ALL, 5) 
     sizer.Add(radio1, 0, wx.ALL, 5) 
     sizer.Add(self.hiddenText, 0, wx.ALL, 5) 
     sizer.Add(self.checkBtn, 0, wx.ALL, 5) 
     sizer.Add(self.hiddenText2, 0, wx.ALL, 5) 
     self.SetSizer(sizer) 

    #---------------------------------------------------------------------- 
    def onRadioButton(self, event): 
     """""" 
     print "in onRadioButton" 
     self.hiddenText.Show() 
     self.Layout() 

    #---------------------------------------------------------------------- 
    def onCheckBox(self, event): 
     """""" 
     print "in onCheckBox" 
     state = event.IsChecked() 
     if state: 
      self.hiddenText2.Show() 
     else: 
      self.hiddenText2.Hide() 
     self.Layout() 


######################################################################## 
class MyFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self, None, title="Radios and Text") 
     panel = MyPanel(self) 
     self.Show() 

if __name__ == "__main__": 
    app = wx.App(False) 
    f = MyFrame() 
    app.MainLoop()