启用父级wx.Frame

时间:2018-11-01 15:52:49

标签: python python-2.7 wxpython

我需要帮助来启用父wx.Frame中的按钮,这里我有一个示例,其中有两个分类的第一类 mainFrame 有两个按钮,第一个通过 m_buttonlogin 启动第二个wx.Frame类实例 loginFrame 和另一个禁用的按钮。 我需要做的是在子框架中成功登录后,禁用按钮在主框架中变为启用状态。

# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc

###########################################################################
## Class mainFrame
###########################################################################

class mainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_buttonlogin = wx.Button( self, wx.ID_ANY, u"Login", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_buttonlogin, 0, wx.ALL, 5 )

        self.m_buttondisabled = wx.Button( self, wx.ID_ANY, u"Dsiabled", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_buttondisabled.Enable( False )

        bSizer1.Add( self.m_buttondisabled, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_buttonlogin.Bind( wx.EVT_BUTTON, self.OnLogin )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def OnLogin( self, event ):
        loginFrame.Show(self)
        event.Skip()


###########################################################################
## Class loginFrame
###########################################################################

class loginFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        self.m_textCtrlusername = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_textCtrlusername, 0, wx.ALL, 5 )

        self.m_textCtrlpassword = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_textCtrlpassword, 0, wx.ALL, 5 )

        self.m_buttonOK = wx.Button( self, wx.ID_ANY, u"OK", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_buttonOK, 0, wx.ALL, 5 )


        self.SetSizer( bSizer2 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_buttonOK.Bind( wx.EVT_BUTTON, self.OnOK )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def OnOK( self, event ):
        # Enable "m_buttondisabled" button in the mainFrame
        event.Skip()

谢谢。

2 个答案:

答案 0 :(得分:1)

一种简单的方法是利用parent中的loginFrame属性。

import wx
import wx.xrc

###########################################################################
## Class mainFrame
###########################################################################

class mainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_buttonlogin = wx.Button( self, wx.ID_ANY, u"Login", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_buttonlogin, 0, wx.ALL, 5 )

        self.m_buttondisabled = wx.Button( self, wx.ID_ANY, u"Disabled", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_buttondisabled.Enable( False )

        bSizer1.Add( self.m_buttondisabled, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_buttonlogin.Bind( wx.EVT_BUTTON, self.OnLogin )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def OnLogin( self, event ):
        loginFrame(self).Show()
        event.Skip()


###########################################################################
## Class loginFrame
###########################################################################

class loginFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.parent = parent
        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )

        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        self.m_textCtrlusername = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_textCtrlusername, 0, wx.ALL, 5 )

        self.m_textCtrlpassword = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_textCtrlpassword, 0, wx.ALL, 5 )

        self.m_buttonOK = wx.Button( self, wx.ID_ANY, u"OK", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_buttonOK, 0, wx.ALL, 5 )


        self.SetSizer( bSizer2 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_buttonOK.Bind( wx.EVT_BUTTON, self.OnOK )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def OnOK( self, event ):
        # Enable "m_buttondisabled" button in the mainFrame
        self.parent.m_buttondisabled.Enable( True )
        self.parent.m_buttonlogin.Enable( False)
        event.Skip()

app = wx.App()
frame = mainFrame(None)
frame.Show()
app.MainLoop()

enter image description here

答案 1 :(得分:0)

有两种方法可以解决此问题。最简单的方法之一是将登录框变成对话框,以便您可以轻松地以模态显示它。然后添加一个类实例属性,可用于确定是否已按下OK按钮。在这种情况下,我将使用self.ok作为属性。

import wx
import wx.xrc

###########################################################################
## Class mainFrame
###########################################################################

class mainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_buttonlogin = wx.Button( self, wx.ID_ANY, u"Login", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_buttonlogin, 0, wx.ALL, 5 )

        self.m_buttondisabled = wx.Button( self, wx.ID_ANY, u"Dsiabled", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_buttondisabled.Enable( False )

        bSizer1.Add( self.m_buttondisabled, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_buttonlogin.Bind( wx.EVT_BUTTON, self.OnLogin )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def OnLogin( self, event ):
        login = loginDialog(self)
        dlg = loginDialog(self)
        dlg.ShowModal()

        if dlg.ok:
            self.m_buttondisabled.Enable(True)

        event.Skip()


###########################################################################
## Class loginFrame
###########################################################################

class loginDialog( wx.Dialog ):

    def __init__( self, parent ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, 
                            size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        self.ok = False

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        self.m_textCtrlusername = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_textCtrlusername, 0, wx.ALL, 5 )

        self.m_textCtrlpassword = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_textCtrlpassword, 0, wx.ALL, 5 )

        self.m_buttonOK = wx.Button( self, wx.ID_ANY, u"OK", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_buttonOK, 0, wx.ALL, 5 )


        self.SetSizer( bSizer2 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_buttonOK.Bind( wx.EVT_BUTTON, self.OnOK )

    def __del__( self ):
        pass

    def MakeModal(self, modal=True):
        if modal and not hasattr(self, '_disabler'):
            self._disabler = wx.WindowDisabler(self)
        if not modal and hasattr(self, '_disabler'):
            del self._disabler

        self.Show()


    # Virtual event handlers, overide them in your derived class
    def OnOK( self, event ):
        self.ok = True
        # Enable "m_buttondisabled" button in the mainFrame
        event.Skip()
        self.Destroy()

if __name__ == '__main__':
    app = wx.App(False)
    frame = mainFrame(None)
    frame.Show()
    app.MainLoop()

这样做,我可以检查对话框实例的ok属性是True还是False,并通过启用(或不启用)按钮来采取相应的行动。

一种替代方法是使用wxPython的内置pubsub模块: