如何为 wxpython 项目模块化代码
how to modularise code for wxpython project
我正在尝试模块化我的代码。下面是从 wxformbuilder 生成的代码..
class MyFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 776,483 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
self.m_menubar1 = wx.MenuBar( 0 )
self.m_menu1 = wx.Menu()
self.m_menubar1.Append( self.m_menu1, u"MyMenu" )
self.m_menu2 = wx.Menu()
self.m_menubar1.Append( self.m_menu2, u"MyMenu2" )
self.m_menu3 = wx.Menu()
self.m_menubar1.Append( self.m_menu3, u"MyMenu3" )
self.SetMenuBar( self.m_menubar1 )
bSizer4 = wx.BoxSizer( wx.HORIZONTAL )
self.splitter_window = wx.SplitterWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SP_3DBORDER )
self.splitter_window.Bind( wx.EVT_IDLE, self.splitter_windowOnIdle )
self.left_window = wx.Panel( self.splitter_window, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.right_window = wx.Panel( self.splitter_window, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_notebook2 = wx.Notebook( self.right_window, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
self.left_panel = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.left_panel, u"a page", False )
self.right_panel = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.right_panel, u"a page", False )
bSizer5.Add( self.m_notebook2, 1, wx.EXPAND |wx.ALL, 5 )
self.right_window.SetSizer( bSizer5 )
self.right_window.Layout()
bSizer5.Fit( self.right_window )
self.splitter_window.SplitVertically( self.left_window, self.right_window, 200 )
bSizer4.Add( self.splitter_window, 1, wx.EXPAND, 5 )
self.SetSizer( bSizer4 )
self.Layout()
self.Centre( wx.BOTH )
def __del__( self ):
pass
def splitter_windowOnIdle( self, event ):
self.splitter_window.SetSashPosition( 200 )
self.splitter_window.Unbind( wx.EVT_IDLE )
这就是我尝试为每个面板创建单独的 类 和 windows
的方式
import wx
import wx.lib.inspection
class l_window(wx.Panel):
def __init__( self , parent):
super().__init__(self, parent)
left_window = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
class r_window(wx.Panel):
def __init__( self , parent):
super().__init__(self, parent)
self.right_window = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_notebook2 = wx.Notebook( self.right_window, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
self.left_panel = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.left_panel, u"a page", False )
self.right_panel = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.right_panel, u"a2 page", False )
bSizer5.Add( self.m_notebook2, 1, wx.EXPAND |wx.ALL, 5 )
class menu_( wx.Menu ):
def __init__(self, parent):
self.m_menubar1 = wx.MenuBar( 0 )
self.m_menu1 = wx.Menu()
self.m_menubar1.Append( self.m_menu1, u"MyMenu" )
self.m_menu2 = wx.Menu()
self.m_menubar1.Append( self.m_menu2, u"MyMenu2" )
self.m_menu3 = wx.Menu()
self.m_menubar1.Append( self.m_menu3, u"MyMenu3" )
class MyFrame( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent,
id = wx.ID_ANY,
title = wx.EmptyString,
pos = wx.DefaultPosition,
size = wx.Size( 900,600 ),
style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetMenuBar( menu_ )
bSizer4 = wx.BoxSizer( wx.HORIZONTAL )
self.splitter_window = wx.SplitterWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SP_3D )
l1 = l_window(self.splitter_window)
r1 = r_window(self.splitter_window)
self.splitter_window.SplitVertically(l1, r1, 200)
bSizer4.Add( self.splitter_window, 1, wx.EXPAND, 5 )
self.SetSizer( bSizer4 )
app = wx.App()
frame = MyFrame(parent=None)
frame.Show()
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
这让我在尝试的不同组合上产生了很多错误...
这里的目标是将 gui 段的代码分解为不同的 类(可以完全放在不同的文件中)以实现长期可管理性。
布局是:top Frame 有菜单栏,还有包含 splitterwindow 的 boxsizer,splitterwindow 有 2 个窗格 left_window 和 right_window。右窗格包含一个笔记本,里面有两个面板。
有什么建议吗?
在短时间使用 wxPython 之后,我了解到结构对于使您能够高效地编码和维护至关重要。对于小型应用程序,一个包含多个 类 的文件就足够了,但是随着控件 and/or 面板和对话框的增加,将代码组织到单独的文件中变得必不可少
之前有人问过类似的问题并回答过(Confused on how to structure the GUI (wxpython))
在一个回答中,我展示了如何将代码组织成 4 个文件,例如
- 应用程序的主框架和应用程序加载器;
- main_panel.py:应用程序的主面板;
- menu_bar.py:框架的菜单栏定义;
- tool_bar.py: 来自框架的工具栏。
我正在尝试模块化我的代码。下面是从 wxformbuilder 生成的代码..
class MyFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 776,483 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
self.m_menubar1 = wx.MenuBar( 0 )
self.m_menu1 = wx.Menu()
self.m_menubar1.Append( self.m_menu1, u"MyMenu" )
self.m_menu2 = wx.Menu()
self.m_menubar1.Append( self.m_menu2, u"MyMenu2" )
self.m_menu3 = wx.Menu()
self.m_menubar1.Append( self.m_menu3, u"MyMenu3" )
self.SetMenuBar( self.m_menubar1 )
bSizer4 = wx.BoxSizer( wx.HORIZONTAL )
self.splitter_window = wx.SplitterWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SP_3DBORDER )
self.splitter_window.Bind( wx.EVT_IDLE, self.splitter_windowOnIdle )
self.left_window = wx.Panel( self.splitter_window, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.right_window = wx.Panel( self.splitter_window, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_notebook2 = wx.Notebook( self.right_window, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
self.left_panel = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.left_panel, u"a page", False )
self.right_panel = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.right_panel, u"a page", False )
bSizer5.Add( self.m_notebook2, 1, wx.EXPAND |wx.ALL, 5 )
self.right_window.SetSizer( bSizer5 )
self.right_window.Layout()
bSizer5.Fit( self.right_window )
self.splitter_window.SplitVertically( self.left_window, self.right_window, 200 )
bSizer4.Add( self.splitter_window, 1, wx.EXPAND, 5 )
self.SetSizer( bSizer4 )
self.Layout()
self.Centre( wx.BOTH )
def __del__( self ):
pass
def splitter_windowOnIdle( self, event ):
self.splitter_window.SetSashPosition( 200 )
self.splitter_window.Unbind( wx.EVT_IDLE )
这就是我尝试为每个面板创建单独的 类 和 windows
的方式import wx
import wx.lib.inspection
class l_window(wx.Panel):
def __init__( self , parent):
super().__init__(self, parent)
left_window = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
class r_window(wx.Panel):
def __init__( self , parent):
super().__init__(self, parent)
self.right_window = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_notebook2 = wx.Notebook( self.right_window, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
self.left_panel = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.left_panel, u"a page", False )
self.right_panel = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.right_panel, u"a2 page", False )
bSizer5.Add( self.m_notebook2, 1, wx.EXPAND |wx.ALL, 5 )
class menu_( wx.Menu ):
def __init__(self, parent):
self.m_menubar1 = wx.MenuBar( 0 )
self.m_menu1 = wx.Menu()
self.m_menubar1.Append( self.m_menu1, u"MyMenu" )
self.m_menu2 = wx.Menu()
self.m_menubar1.Append( self.m_menu2, u"MyMenu2" )
self.m_menu3 = wx.Menu()
self.m_menubar1.Append( self.m_menu3, u"MyMenu3" )
class MyFrame( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent,
id = wx.ID_ANY,
title = wx.EmptyString,
pos = wx.DefaultPosition,
size = wx.Size( 900,600 ),
style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetMenuBar( menu_ )
bSizer4 = wx.BoxSizer( wx.HORIZONTAL )
self.splitter_window = wx.SplitterWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SP_3D )
l1 = l_window(self.splitter_window)
r1 = r_window(self.splitter_window)
self.splitter_window.SplitVertically(l1, r1, 200)
bSizer4.Add( self.splitter_window, 1, wx.EXPAND, 5 )
self.SetSizer( bSizer4 )
app = wx.App()
frame = MyFrame(parent=None)
frame.Show()
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
这让我在尝试的不同组合上产生了很多错误...
这里的目标是将 gui 段的代码分解为不同的 类(可以完全放在不同的文件中)以实现长期可管理性。
布局是:top Frame 有菜单栏,还有包含 splitterwindow 的 boxsizer,splitterwindow 有 2 个窗格 left_window 和 right_window。右窗格包含一个笔记本,里面有两个面板。
有什么建议吗?
在短时间使用 wxPython 之后,我了解到结构对于使您能够高效地编码和维护至关重要。对于小型应用程序,一个包含多个 类 的文件就足够了,但是随着控件 and/or 面板和对话框的增加,将代码组织到单独的文件中变得必不可少
之前有人问过类似的问题并回答过(Confused on how to structure the GUI (wxpython))
在一个回答中,我展示了如何将代码组织成 4 个文件,例如
- 应用程序的主框架和应用程序加载器;
- main_panel.py:应用程序的主面板;
- menu_bar.py:框架的菜单栏定义;
- tool_bar.py: 来自框架的工具栏。