gpt4 book ai didi

python - wxpython - 一个框架,多个面板,模块化代码

转载 作者:太空狗 更新时间:2023-10-30 02:11:29 24 4
gpt4 key购买 nike

我正在处理一个相当大的 GUI 项目,因此我使用 wxpython 来构建它。我有一个框架,多个框架具有自己的功能和一个导入 gui 组件以及其他外部功能的主文件。我决定让 gui/wxpython 代码保持不同,以便更好地模块化代码。

我的主要问题是如何在单独的面板中执行函数以及如何使它们在我的主 python 文件中工作。

下面是我的 wxpython 代码示例:

import wx

class 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 ):
pass


class panel_one ( wx.Panel ):

def __init__( self, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), 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, 0, 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, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL )

... some code in here ...

def __del__( self ):
pass

这些是我的图形用户界面组件。然后,在我的主文件中,我将其导入并运行 gui:

import gui

class 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 wx

class 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 ):
pass

class panel_one ( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"panel 1 button", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer5.Add( self.m_button2, 0, 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, parent ):
wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"panel 2 button ", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer5.Add( self.m_button2, 0, 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()

主应用程序.py

import wx
import gui

class 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)
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, parent):
gui.panel_two.__init__(self, parent)
self.parent = parent

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

关于python - wxpython - 一个框架,多个面板,模块化代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21899151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com