简体   繁体   中英

Wrong layout when porting dialog code to python 3 and phoenix

I am porting my old python 2.7 code to 3.6 with wxpython phoenix and struggling now with a wxdialog where I don't succeed to get the correct layout. All wigdets appear in the upper left corner of the window. Can anybody help?

I work with Python 3.6.4, wxpython 4.0.1 msw (phoenix). The XRC file was generated with wxFormBuilder 3.5.

This is what I expect (shown by wxFormBuilder):

预期的对话框布局

And this is what I get:

实际的对话框布局

Thank you in advance

Regards

Edit I tried to get the sizer containing the button by self.m_button_ok.GetContainingSizer() . This method returns None! Does that mean that there are no valuable Sizers?

Here is my code:

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()

And the XRC file:

<?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>

Found a solution, this code works. Anyhow I have the feeling that it is not the right way, since CTestDialog is no more a wxDialog class. The comments in the code explain what I changed.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM