2011-11-06 62 views
1

這裏的想法是登錄(頂框稱爲LoginFrame)。 用2個選項(a或b)打開子框架(MainFrame)。 每個選項打開另一個子框架(以MainFrame作爲父框架)。 每個內部都有一個返回到MainFrame的按鈕。「爺爺框架內的父框架內的兒童框架」wxPython

我使用LoginFrame作爲MainFrame的父項,a和b作爲解決方法。 但我無法獲得後退按鈕。

class MainFrame(wx.Frame): 
    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id,'Title',size=(353,270),style=wx.CAPTION|wx.CLOSE_BOX|wx.SYSTEM_MENU|wx.MINIMIZE_BOX) 
     self.panel=wx.Panel(self) 

     aButton= wx.Button(self.panel, -1, 'a',pos=(10,10),size=(-1,30)) 
     self.Bind(wx.EVT_BUTTON, self.a,aButton) 

     bButton= wx.Button(self.panel, -1, 'b',pos=(100,10),size=(-1,30)) 
     self.Bind(wx.EVT_BUTTON, self.b,bButton) 

    def a(self,event): 
     aframe=aFrame(parent=frame,id=997) 
     aframe.Centre() 
     aframe.Show() 
     mainframe.Hide() 

    def b(self,event): 
     bframe=bFrame(parent=frame,id=996) 
     bframe.Centre() 
     bframe.Show() 
     mainframe.Hide() 


class bFrame(wx.Frame): 
    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id,'2',size=(353,270),style=wx.CAPTION|wx.CLOSE_BOX|wx.SYSTEM_MENU|wx.MINIMIZE_BOX) 
     self.panel=wx.Panel(self) 

     mainButton = wx.Button(self.panel, -1, '&Back to Main',pos=(100,100),size=(-1,30)) 
     self.Bind(wx.EVT_BUTTON, self.backMain,mainButton) 

    def backMain (self, event): 
     mainframe.Show() 
     self.Destroy() 


class aFrame(wx.Frame): 
    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id,'1',size=(353,270),style=wx.CAPTION|wx.CLOSE_BOX|wx.SYSTEM_MENU|wx.MINIMIZE_BOX) 
     self.panel=wx.Panel(self) 

     mainButton = wx.Button(self.panel, -1, '&Back to Main',pos=(100,100),size=(-1,30)) 
     self.Bind(wx.EVT_BUTTON, self.backMain,mainButton) 

    def backMain (self, event): 
     mainframe.Show() 
     self.Destroy() 


class LoginFrame(wx.Frame): 
    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id,'Login',size=(400,200),style=wx.CAPTION|wx.CLOSE_BOX|wx.SYSTEM_MENU|wx.MINIMIZE_BOX) 
     self.panel=wx.Panel(self) 
     sizer = wx.FlexGridSizer(rows=3, cols=2, hgap=5, vgap=15) 

     self.txt_Username = wx.TextCtrl(self.panel, 1, size=(150, -1)) 
     username = wx.StaticText(self.panel, -1, "Username:") 
     sizer.Add(username,0, wx.LEFT|wx.TOP| wx.RIGHT, 50) 
     sizer.Add(self.txt_Username,0, wx.TOP| wx.RIGHT, 50) 

     self.txt_Password = wx.TextCtrl(self.panel, 1, size=(150, -1), style=wx.TE_PASSWORD) 
     password = wx.StaticText(self.panel, -1, "Password:") 
     sizer.Add(password,0, wx.LEFT|wx.RIGHT, 50) 
     sizer.Add(self.txt_Password,0, wx.RIGHT, 50) 

     loginButton = wx.Button(self.panel, -1, "&Login") 
     self.Bind(wx.EVT_BUTTON, self.login,loginButton) 
     sizer.Add(loginButton,0, wx.LEFT, 50) 

     self.panel.SetSizer(sizer) 

    def login(self, event): 
     usertext = self.txt_Username.GetValue() 
     passwordtext = self.txt_Password.GetValue() 
     if usertext=='' and passwordtext=='': 
      granted=wx.MessageDialog(None,'Access Granted!','Access Granted!',wx.OK) 
      answerG=granted.ShowModal() 
      granted.Destroy() 
      mainframe=MainFrame(parent=frame,id=998) 
      mainframe.Centre() 
      mainframe.Show() 
      frame.Hide() 
     else: 
      denied=wx.MessageDialog(None,'Access Denied!','Access Denied!',wx.OK) 
      answerD=denied.ShowModal() 
      denied.Destroy() 


if __name__ == '__main__': 
    app=wx.App() 
    frame=LoginFrame(parent=None,id=999) 
    frame.Centre() 
    frame.Show() 
    app.MainLoop() 

回答

0

修改方法a和b是這樣的:

def a(self,event): 
    aframe = aFrame(parent=self, id=997) # parent is the MainFrame instance self 
    aframe.Centre() 
    aframe.Show() 
    self.Hide()       # Hide the MainFrame instance 

然後修改幀A和B是這樣的:

class bFrame(wx.Frame): 
    def __init__(self, parent, id): 
     wx.Frame.__init__(self, parent, id, '2', size=(353,270), style=wx.CAPTION|wx.CLOSE_BOX|wx.SYSTEM_MENU|wx.MINIMIZE_BOX) 
     self.parent = parent    # parent is the MainFrame instance (self --> parent) 
     self.panel = wx.Panel(self) 

     mainButton = wx.Button(self.panel, -1, '&Back to Main',pos=(100,100),size=(-1,30)) 
     self.Bind(wx.EVT_BUTTON, self.backMain,mainButton) 

    def backMain (self, event): 
     self.parent.Show()    # now I can show again Mainframe 
     self.Destroy() 
+0

完美。非常感謝你 :) –