【发布时间】:2018-05-17 10:01:53
【问题描述】:
我是 wxpython 的新手。
我的目的是生成一个由两个带有特定控件的水平面板组成的 UI。我从 wx.Panel 派生了我想要集成到 mainFrame 的 controlPanel。 controlPanel 由一个 wx.staticBox 和两个“子” wx.StaticBox 组成,其中添加了一些控件。 不知何故,当窗口打开时,控件显示在 staticBox 的左上角,如图所示on this image
你能从那一堆代码中解释为什么控件没有分布在 staticBoxes 中吗?
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)
主框架:
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)
【问题讨论】:
-
self.m_inputDataPathStruct = namedtuple("staticText", "editBox", "browseBn")是怎么回事。为什么不直接将它们声明为wx对象? -
这 3 个控件(staticText、editBox、browseBn)是为相同目的而构建的。因此我将它们存储在相同的结构中。
-
这段代码有效吗?我得到“TypeError:namedtuple() 接受 2 个位置参数,但给出了 3 个”
标签: python user-interface wxpython