我的主要问题是如何在单独的面板中执行这些功能以及如何使它们从我的主python文件中运行.
下面是我的wxpython代码示例:
import wxclass 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( 800,600 ),style = wx.DEFAulT_FRAME_STYLE|wx.TAB_TRAVERSAL ) self.SetSizeHintsSz( wx.DefaultSize,wx.DefaultSize ) bSizer1 = wx.BoxSizer( wx.VERTICAL ) self.SetSizer( bSizer1 ) self.Layout() self.panelOne = panel_one(self) self.panelTwo = panel_two(self) self.panelTwo.HIDe() self.Centre( wx.BOTH ) def __del__( self ): passclass panel_one ( wx.Panel ): def __init__( self,parent ): wx.Panel.__init__ ( self,style = wx.TAB_TRAVERSAL ) bSizer5 = wx.BoxSizer( wx.VERTICAL ) self.m_button2 = wx.button( self,wx.ID_ANY,u"Mybutton",wx.Defaultposition,wx.DefaultSize,0 ) bSizer5.Add( self.m_button2,wx.ALL,5 ) self.SetSizer( bSizer5 ) self.Layout() # Connect Events self.m_button2.Bind( wx.EVT_button,self.changeIntroPanel ) def __del__( self ): pass # Virtual event handlers,overIDe them in your derived class def changeIntroPanel( self,event ): event.Skip()class panel_two ( wx.Panel ): def __init__( self,style = wx.TAB_TRAVERSAL ) ... some code in here ... def __del__( self ): pass
所以那些是我的gui组件.然后,在我的主文件中,我导入它并运行gui:
import guiclass MainApp(gui.MainFrame): def __init__(self,parent): gui.MainFrame.__init__(self,parent) self.panelOne = Panel1(self) self.panelTwo = Panel2(self) self.panelTwo.HIDe()class Panel1(gui.panel_one): def __init__(self,parent): gui.panel_one.__init__(self,parent) def changeIntroPanel( self,event ): if self.panelOne.IsShown(): self.SetTitle("Panel Two Showing") self.PanelOne.HIDe() self.PanelTwo.Show() else: self.SetTitle("Panel One Showing") self.PanelOne.Show() self.PanelTwo.HIDe() self.Layout()class Panel2(gui.panel_two): def __init__(self,parent): gui.panel_two.__init__(self,parent)def main(): app = wx.App() window = MainApp(None) window.Show(True) app.MainLoop()if __name__ == '__main__': main()
正如你所知,我的想法是我想在我的主文件中完成所有实现.我将如何定义我在第一次在panel_one中定义的changeIntroPanel函数的功能?目前,我们的想法是隐藏panelOne并显示panelTwo.
我之前做过类似的事情,但只有一帧.在这种情况下,它是微不足道的,因为所有函数都在MainFrame类中开始.在这种情况下,我发布的代码将完美地工作.
任何帮助表示赞赏.谢谢.
解决方法 def OnInit(self,parent):用于wx.App,你需要def _ _init_ _(self,parent).关于__init__请检查:Using inheritance in python
关于__init__和OnInit之间的区别,请查看this link
编辑:
gui.py
import wxclass MainFrame ( wx.Frame ): def __init__( self,style = wx.DEFAulT_FRAME_STYLE|wx.TAB_TRAVERSAL ) self.SetSizeHintsSz( wx.DefaultSize,wx.DefaultSize ) bSizer1 = wx.BoxSizer( wx.VERTICAL ) self.SetSizer( bSizer1 ) self.Layout() # self.panelOne = panel_one(self) # self.panelTwo = panel_two(self) # self.panelTwo.HIDe() self.Centre( wx.BOTH ) def __del__( self ): passclass panel_one ( wx.Panel ): def __init__( self,style = wx.TAB_TRAVERSAL ) bSizer5 = wx.BoxSizer( wx.VERTICAL ) self.m_button2 = wx.button( self,u"panel 1 button",5 ) self.SetSizer( bSizer5 ) self.Layout() # Connect Events self.m_button2.Bind( wx.EVT_button,self.changeIntroPanel ) def __del__( self ): pass # Virtual event handlers,event ): event.Skip()class panel_two ( wx.Panel ): def __init__( self,u"panel 2 button ",self.changeIntroPanel ) def __del__( self ): pass # Virtual event handlers,event ): event.Skip()
mainapp.py
import wximport guiclass MainApp(gui.MainFrame): def __init__(self,parent) self.parent = parent def changeIntroPanel( self,event ): if self.IsShown(): self.parent.SetTitle("Panel Two Showing") self.HIDe() self.parent.panelTwo.Show()class Panel2(gui.panel_two): def __init__(self,event ): if self.IsShown(): self.parent.SetTitle("Panel One Showing") self.parent.panelOne.Show() self.HIDe()def main(): app = wx.App() window = MainApp(None) window.Show(True) app.MainLoop()if __name__ == '__main__': main()总结
以上是内存溢出为你收集整理的wxpython – 一个框架,多个面板,模块化代码全部内容,希望文章能够帮你解决wxpython – 一个框架,多个面板,模块化代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)