Python 在xrc创建的对话框中替换小部件

Python 在xrc创建的对话框中替换小部件,python,python-2.7,dialog,wxpython,xrc,Python,Python 2.7,Dialog,Wxpython,Xrc,我想动态替换对话框的对象,但没有找到方法。该对话框是在xrc定义文件的帮助下创建的 我发现的是:需要访问sizer,但据我所知,xrc不提供对sizer对象的访问 有人能帮忙吗 我的系统:Python2.7、Wxpython3.0.2.0、win7 致意 亨巴兰在高雄帖子的帮助下,我找到了以下解决方案: def exchange_control( self, control, coded ) : """ Exchanges a wx.TextCtrl (where wx.TE_PASSWORD

我想动态替换对话框的对象,但没有找到方法。该对话框是在xrc定义文件的帮助下创建的

我发现的是:需要访问sizer,但据我所知,xrc不提供对sizer对象的访问

有人能帮忙吗

我的系统:Python2.7、Wxpython3.0.2.0、win7

致意
亨巴兰

在高雄帖子的帮助下,我找到了以下解决方案:

def exchange_control( self, control, coded ) :
"""
Exchanges a wx.TextCtrl (where wx.TE_PASSWORD is set) with a wx.TextCtrl (where
wx.TE_PASSWORD is not set) and vice versa. A similar code could be used to exchange
for exampe a DirPickerCtrl with a FilePickerCtrl.

:param wx.TextCtrl control: Contains a coded or encoded text dependend on `coded`.
:param bool coded: True if the text in `control` is coded, False if it is encoded.

:returns: 
    A new wx.TextCtrl with the coded (`coded` was False) or encoded Text (`coded` was 
    True) of `control`.
:rtype: wx.TextCtrl
"""
    containing_sizer = control.GetContainingSizer() # get the sizer containing `control`

    # Compute arguments for a new TextCtrl. 
    # - The value of style is arbitrary except the TE_PASSWORD part. 
    # - a_validator is a validator checking correctness of the value of `control`, is 
    #   optional.
    kwargs = { 'parent'   : control.Parent,
               'id'       : wx.ID_ANY,
               'value'    : control.GetValue(),
               'size'     : control.Size,
               'style'    : wx.TE_NO_VSCROLL|wx.TE_PASSWORD if coded  else wx.TE_NO_VSCROLL,
               'name'     : control.Name,
               'validator': a_validator
             }

    # Setup a new TextCtrl with the arguments kwargs
    new_text_ctrl = wx.TextCtrl( **kwargs )

    containing_sizer.Replace( control, new_text_ctrl )
    containing_sizer.Layout()

    return new_text_ctrl

# In the calling code:
# The returned `the_text_ctrl` is the TextCtrl Field with the (en)coded Text and `coded` 
# is  the boolean value  which shows if the text whithin this field is coded or encoded. 
the_text_ctrl = self.exchange_control( the_text_ctrl, coded )
编辑:
我更改了代码,因为我发现Replace()方法比我第一次尝试时更容易使用

Sizer与其他小部件有何不同?Sizer在XRC中没有名称。如果我添加一个,它将被忽略。示例:XRC:。实例化该代码时使用self.my_text_ctrl=wx.xrc.XRCCTRL(self,“myTextCtrl”),XRCCTRL返回wx.TextCtrl的实例。另一方面,如果我用self.my_grid_bag=wx.xrc.XRCCTRL(self,“myGridBag”)声明和实例化,XRCCTRL将不返回任何值。对于否决我问题的人:我总是想学习如何做得更好。你能告诉我你为什么投了否决票吗?这很旧,但我想事情并没有多大改善。谢谢,高友,这正是我需要的。在布局上仍有一些困难,但我希望能尽快解决。如果你把你的建议复制到答案栏,我可以把我的问题标记为“已解决”