我对wxpython很新。
我的目的是生成一个由两个具有特定控件的水平面板组成的UI。我从wx.Panel派生了controlPanel,我希望将它集成到mainFrame中。 controlPanel由一个wx.staticBox和两个" sub"组成。 wx.StaticBox,其中添加了一些控件。 不知何故,当窗口打开时,控件显示在staticBox的左上角,如图on this image所示
你能解释为什么控件没有从那堆代码中的staticBox中分发:
import wx
from wx._core import ID_ANY
from collections import namedtuple
class mvtControlPanelUI(wx.Panel):
m_mainCtrBoxSizer = []
m_mainCtrStaticBox = []
m_commonControlsBox = []
m_customControlsBox = []
m_inputDataPathStruct = []
m_outputDataPathStruct = []
m_commonCtrlSizer = []
m_inputDataCtrlSizer = []
m_outputDataCtrlSizer = []
def __init__(self, parent):
wx.Panel.__init__(self, parent, name="controlPanel")
self.SetBackgroundColour('white')
self.m_mainCtrStaticBox = wx.StaticBox(self, label = "Controls")
self.m_mainCtrBoxSizer = wx.StaticBoxSizer(self.m_mainCtrStaticBox,
wx.VERTICAL)
# common controls
self.initCustomControls()
self.initCommonControls()
self.SetSizer(self.m_mainCtrBoxSizer)
self.Layout()
# common control initialization
def initCommonControls(self):
#input data controls
self.m_commonControlsBox = wx.StaticBox(self.m_mainCtrStaticBox, -1, "Common controls");
#common control sizer
self.m_commonCtrlSizer = wx.StaticBoxSizer(self.m_commonControlsBox, wx.VERTICAL)
#input data controls sizer
self.m_inputDataCtrlSizer = wx.BoxSizer(wx.HORIZONTAL)
# Create the struct
self.m_inputDataPathStruct = namedtuple("staticText", "editBox", "browseBn")
# static text
self.m_inputDataPathStruct.staticText = wx.StaticText(self.m_commonControlsBox, -1, "Input data path:", style = wx.ALIGN_LEFT)
self.m_inputDataCtrlSizer.Add(self.m_inputDataPathStruct.staticText, 0, wx.ALL|wx.CENTER, 5)
# text box
self.m_inputDataPathStruct.editBox = wx.TextCtrl(self.m_commonControlsBox, -1, "/path/to/data/folder", style = wx.ALIGN_LEFT)
self.m_inputDataCtrlSizer.Add(self.m_inputDataPathStruct.editBox, 0, wx.ALL|wx.CENTER, 5)
# browse button
self.m_inputDataPathStruct.browseBn = wx.Button(self.m_commonControlsBox, -1, "...", style = wx.ALIGN_LEFT)
self.m_inputDataCtrlSizer.Add(self.m_inputDataPathStruct.browseBn, 0, wx.ALL|wx.CENTER, 5)
# insert input controls sizer in common control sizer
self.m_commonCtrlSizer.Add(self.m_inputDataCtrlSizer, 1, wx.ALL|wx.CENTER, 10)
#add common control box to the mainControl box sizer
self.m_mainCtrBoxSizer.Add(self.m_commonControlsBox, 1, wx.ALL|wx.EXPAND, 10)
#
def initCustomControls(self):
self.m_customControlsBox = wx.StaticBox(self.m_mainCtrStaticBox, -1, "custom controls");
#custom control sizer
self.m_customCtrlSizer = wx.StaticBoxSizer(self.m_customControlsBox, wx.VERTICAL)
#static text
staticText = wx.StaticText(self.m_customControlsBox, -1, "custom Controls go Here", style = wx.ALIGN_LEFT)
self.m_customCtrlSizer.Add(staticText, 1, wx.ALL|wx.CENTER, 10)
self.m_mainCtrBoxSizer.Add(self.m_customControlsBox, 4, wx.ALL|wx.EXPAND, 10)
MainFrame:
import wx
import baseUI.mvtControlPanelUI as cPnl
import baseUI.mvtResultsPanelUI as rPnl
TBFLAGS = ( wx.TB_HORIZONTAL
| wx.NO_BORDER
| wx.TB_FLAT
#| wx.TB_TEXT
#| wx.TB_HORZ_LAYOUT
)
class mvtMainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, wx.ID_ANY, 'myAwesomeFrame', size=(600, 400))
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
# panels
mainPanel = wx.Panel(self)
controlPnl = cPnl.mvtControlPanelUI(mainPanel)
resultsPnl = rPnl.mvtResultsPanelUI(mainPanel)
# Main Panel sizer wraps control and results panels
mainFrameSizer = wx.BoxSizer(wx.VERTICAL)
mainPnlSizer = wx.BoxSizer(wx.HORIZONTAL)
mainPnlSizer.Add(controlPnl, proportion = 1, flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP)
mainPnlSizer.Add(resultsPnl, proportion = 3, flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP)
mainPanel.SetSizer(mainFrameSizer)
# mainFrameSizer.Add(self._ribbon, 0, wx.EXPAND)
mainFrameSizer.Add(mainPnlSizer, 1, wx.EXPAND)
self.CreateStatusBar()
self.Layout()
def OnCloseWindow(self, event):
self.Destroy()
def OnFullScreen(self, event):
self.ShowFullScreen(not self.IsFullScreen(), 0)
答案 0 :(得分:1)
下面的代码给出了我认为您正在寻找的内容(仅适用于其中一个面板) 代码中的问题与分配父窗口小部件和子窗口小部件有关 另一方面,我的建议是你不应该比你需要的代码更复杂。很长的名字,过于冗长的名字,有时可以隐藏代码结构 您还可以利用wxglade等应用程序以更“安全”和干净的方式构建UI
import wx
class ControlPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, name="controlPanel")
self.SetBackgroundColour('white')
self.mainBox = wx.StaticBox(self, label ="Controls")
self.mainSizer = wx.StaticBoxSizer(self.mainBox, wx.VERTICAL)
self.initCustomControls()
self.initCommonControls()
self.SetSizer(self.mainSizer)
self.Layout()
def initCommonControls(self):
self.commonBox = wx.StaticBox(self, -1, "Common controls");
self.commonSizer = wx.StaticBoxSizer(self.commonBox, wx.VERTICAL)
self.text = wx.StaticText(self, -1, "Input data path:", style = wx.ALIGN_LEFT)
self.editBox = wx.TextCtrl(self, -1, "/path/to/data/folder", style=wx.ALIGN_LEFT)
self.browseBn = wx.Button(self, -1, "...", style=wx.ALIGN_LEFT)
self.commonSizer.Add(self.text, 0, wx.ALL | wx.CENTER, 5)
self.commonSizer.Add(self.editBox, 0, wx.ALL | wx.CENTER, 5)
self.commonSizer.Add(self.browseBn, 0, wx.ALL | wx.CENTER, 5)
self.mainSizer.Add(self.commonSizer, 1, wx.ALL | wx.EXPAND, 10)
def initCustomControls(self):
self.customBox = wx.StaticBox(self, -1, "custom controls");
self.customSizer = wx.StaticBoxSizer(self.customBox, wx.VERTICAL)
staticText = wx.StaticText(self, -1, "custom Controls go Here", style = wx.ALIGN_LEFT)
self.customSizer.Add(staticText, 1, wx.ALL|wx.CENTER, 10)
self.mainSizer.Add(self.customSizer, 4, wx.ALL | wx.EXPAND, 10)
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, wx.ID_ANY, 'myAwesomeFrame', size=(400, 400))
controlPnl = ControlPanel(self)
mainPnlSizer = wx.BoxSizer(wx.HORIZONTAL)
mainPnlSizer.Add(controlPnl, proportion = 1, flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP)
self.SetSizer(mainPnlSizer)
self.CreateStatusBar()
self.Layout()
if __name__ == "__main__":
app = wx.App()
f = MyFrame(None)
f.Show(True)
app.MainLoop()
你得到: