“子框架在父亲框架内,祖父框架在框架内” wxPython

发布于 2024-12-14 03:45:20 字数 3645 浏览 0 评论 0原文

这里的想法是登录(顶部框架称为 LoginFrame)。 打开具有 2 个选项(a 或 b)的子框架(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()

The idea here is to login (top frame called LoginFrame).
Open a child frame (MainFrame) with 2 option (a or b).
Each option opens another child frame (with MainFrame as parent).
Inside each one is a button to go back to the MainFrame.

I used the LoginFrame as parent for MainFrame,a and b as a workaround.
But I can't get the back button to work.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-12-21 03:45:20

像这样修改方法 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()

Modify the methods a and b like this:

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

Then modify the Frames a and b like this:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文