“爷爷框架内的父框架内的儿童框架”wxPython

时间:2011-11-06 20:23:18

标签: python wxpython

这里的想法是登录(名为LoginFrame的顶部框架)。 使用2个选项(a或b)打开子框架(MainFrame)。 每个选项都会打开另一个子框架(MainFrame作为父框架)。 每个内部都有一个返回MainFrame的按钮。

我使用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()

1 个答案:

答案 0 :(得分: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()