默认情况下隐藏wxpython中的TextCtrl

时间:2012-12-05 14:23:27

标签: wxpython

我想要一个放在水平框内的textctrl。 我使用以下代码创建了这个:

self.myTextCtrl = wx.TextCtrl(panel,-1,“bleh”)

self.vbox.Add(self.myTextCtrl,proportion = 1)

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

但是,我上方有一个radiobutton(默认为false),当我将其设置为true时,我想要显示该框。 我试着打电话 self.myTextCtrl.Hide()

(隐藏发生在由无线电触发但切换的事件中)

但这会导致textctrl无法在以后加载...

Some1告诉我,这与wxpython编译你的程序有关,因为你没有使用它,但我无法在网上找到相关信息。

请帮忙。

1 个答案:

答案 0 :(得分: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()