2012-12-05 146 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()