“子框架在父亲框架内,祖父框架在框架内” wxPython
这里的想法是登录(顶部框架称为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样修改方法 a 和 b:
然后像这样修改框架 a 和 b:
Modify the methods a and b like this:
Then modify the Frames a and b like this: