0

我正在使用 wxpython phoenix 将旧的 python 2.7 代码移植到 3.6,现在正在使用 wxdialog 苦苦挣扎,我无法成功获得正确的布局。所有 wigdets 都出现在窗口的左上角。有人可以帮忙吗?

我使用 Python 3.6.4、wxpython 4.0.1 msw (phoenix)。XRC 文件是使用 wxFormBuilder 3.5 生成的。

这是我所期望的(由 wxFormBuilder 显示):

预期的对话框布局

这就是我得到的:

实际对话框布局

先感谢您

问候

编辑 我试图通过self.m_button_ok.GetContainingSizer(). 此方法返回无!这是否意味着没有有价值的 Sizer?

这是我的代码:

import wx
import wx.xrc

class CTestDialog( wx.Dialog ):

    def __init__( self, parent ):

        wx.Dialog.__init__( self )
        self.Create( parent, wx.ID_ANY, 'my Dlg Title' )

        self._resources = wx.xrc.XmlResource( 'TestDialog.xrc' )
        self._resources.LoadDialog( self, 'CTestDialog' )

        self.m_button_ok = self._resources.LoadObjectRecursively( self, 'm_button_ok', 'wxButton' )
        self.m_textCtrl_name = self._resources.LoadObjectRecursively( self, 'm_textCtrl_name', 'wxTextCtrl' )

        button_sizer = self.m_button_ok.GetContainingSizer() # button_sizer is None

        self.Layout()       

if __name__ == "__main__":
    app = wx.App( redirect=False )
    dlg = CTestDialog( None )
    dlg.ShowModal()

XRC 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<resource xmlns="http://www.wxwindows.org/wxxrc" version="2.3.0.1">
    <object class="wxDialog" name="CTestDialog">
        <style>wxDEFAULT_DIALOG_STYLE</style>
        <title>Test</title>
        <centered>1</centered>
        <object class="wxBoxSizer">
            <orient>wxVERTICAL</orient>
            <object class="sizeritem">
                <option>1</option>
                <flag>wxEXPAND|wxLEFT|wxRIGHT|wxTOP</flag>
                <border>5</border>
                <object class="wxBoxSizer">
                    <orient>wxHORIZONTAL</orient>
                    <object class="sizeritem">
                        <option>0</option>
                        <flag>wxLEFT|wxTOP</flag>
                        <border>3</border>
                        <object class="wxStaticText" name="m_staticText_name">
                            <size>80,-1</size>
                            <label>Name</label>
                            <wrap>-1</wrap>
                        </object>
                    </object>
                    <object class="sizeritem">
                        <option>0</option>
                        <flag>wxBOTTOM|wxRIGHT</flag>
                        <border>3</border>
                        <object class="wxTextCtrl" name="m_textCtrl_name">
                            <size>180,-1</size>
                            <value></value>
                        </object>
                    </object>
                </object>
            </object>
            <object class="sizeritem">
                <option>1</option>
                <flag>wxALIGN_RIGHT|wxALL|wxEXPAND</flag>
                <border>5</border>
                <object class="wxBoxSizer">
                    <orient>wxHORIZONTAL</orient>
                    <object class="sizeritem">
                        <option>0</option>
                        <flag>wxLEFT</flag>
                        <border>50</border>
                        <object class="wxButton" name="m_button_ok">
                            <label>_OK</label>
                            <default>0</default>
                        </object>
                    </object>
                </object>
            </object>
        </object>
    </object>
</resource>
4

1 回答 1

0

找到了解决方案,此代码有效。无论如何,我觉得这不是正确的方法,因为CTestDialog不再是一wxDialog门课。代码中的注释解释了我所做的更改。

import wx
import wx.xrc

class CTestDialog( object ): # New: this is no more a wxDialog object but it  contains one

    def __init__( self, parent ):

        #wx.Dialog.__init__( self, None, wx.ID_ANY, 'CTestDialog' )
        #self.Create( parent, wx.ID_ANY, 'my Dlg Title' )

        self._resources = wx.xrc.XmlResource( 'TestDialog.xrc' )
        self._dlg = self._resources.LoadDialog( None, 'CTestDialog' ) # New: assign to _dlg

        self.m_button_ok = wx.xrc.XRCCTRL( self._dlg , 'm_button_ok' )  # New: XRCCTRL
        self.m_textCtrl_name = wx.xrc.XRCCTRL( self._dlg , 'm_textCtrl_name' ) # New: XRCCTRL

        self._dlg.Layout() # New: refers to _dlg, no more to self


    def ShowModal( self ):
        # New: refers to _dlg
        return self._dlg.ShowModal()


if __name__ == "__main__":
    app = wx.App( redirect=False )
    dlg = CTestDialog( None )
    dlg.ShowModal()
于 2018-03-09T14:32:57.860 回答