簡答回答:您可能有也可能沒有父面板,但您沒有正確設置尺寸。由於Windows上的wx.Frame
具有深灰色背景,因此如果您不使用wx.Panel
(並使其尺寸正確),它將發光。
長答案:試圖從您的代碼中包含一個最小的運行示例。它看起來很醜陋(TextCtrl
和Button
),因爲定義panel
來自外部,並且很難同步它們的尺寸。在全部元素創建/調整在一個wx.Panel
/wx.Frame
將是好得多。我所有對你的代碼的評論都包含在三重引號中,並且應該讓你明白爲什麼你要做的事情很複雜/不好/簡單不起作用。
import wx
class panel_class(wx.Panel):
def __init__(self, *args, **kwds):
"""Trying to implement your magic ``panel```"""
wx.Panel.__init__(self, *args, **kwds)
pnl = self
szmain = wx.BoxSizer(wx.HORIZONTAL)
szmain.Add(wx.TextCtrl(pnl, -1, 'Database path'), 1, wx.EXPAND)
szmain.Add(wx.Button(pnl, -1, 'Database path'), 0, wx.EXPAND)
pnl.SetSizer(szmain)
class myframe(wx.Frame):
def __init__(self, *args, **kwds):
"""Constructor"""
wx.Frame.__init__(self, *args, **kwds)#None, title="Title Options", size=(520, 390))
prefPanel = wx.Panel(self)
"""This makes no sense: according to the wx.Frame.__init__ above self is a wx.Frame."""
# prefPanel = self
# SET THE SIZER OBJECT UP
prefSizer = wx.GridBagSizer(13, 9)
# SET BASELINE INDEX VARIABLES
xIndex = 0
yIndex = 0
# DEFAULT DATABSE EXTENSIONS
self.tc_DBExt = wx.TextCtrl(prefPanel, -1, "", (0,0), (150,21))
self.label_DBExt = wx.StaticText(prefPanel, label="Default Database Extensions:")
help_tc_DBExt= "Enter this as: *.<extension>"
self.tc_DBExt.SetToolTip(wx.ToolTip(help_tc_DBExt))
"""Replaced with something static."""
self.tc_DBExt.Value = 'conftxt'#guiFunctions.configMe("general", "database_extensions")
prefSizer.Add(self.label_DBExt, pos=(xIndex, 0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL|wx.TOP, border=10)
prefSizer.Add(self.tc_DBExt, pos=(xIndex, 1), span=(1,5), flag=wx.EXPAND|wx.LEFT|wx.ALIGN_CENTER_VERTICAL|wx.TOP, border=10).SetMinSize((200,22))
"""Assuming panel definition from outside.
It has to be created here. If it is to be created **before**
the parent is created, it is very complicated to create it with a
dummy parent and ``Reparent`` it to this Frame/Panel. Don't do that."""
panel = panel_class(prefPanel, -1)
"""Attach ``panel`` to a sizer in **this** hierarchy
It looks disconnected, because it has brought already it's own
sizer, which is child to the GridBagSizer."""
xindex = 1
prefSizer.Add(panel, pos=(xindex, 0), span=(1,5), flag=wx.EXPAND|wx.LEFT|wx.ALIGN_CENTER_VERTICAL|wx.TOP, border=10).SetMinSize((200,22))
"""You have to tell wxPython somehow to what element the sizer is coupled (in this case to the main pane)l"""
prefPanel.SetSizer(prefSizer)
"""I have no idea """
#panel.SetSizer(sizer2)
#sizer2.Fit(panel)
"""Normally not needed/happening automagically on standard controls"""
#panel.Refresh()
#panel.Update()
#panel.Layout()
def add_panel_to_layout(self):
pass
if __name__ == '__main__':
app = wx.App(0)
frm = myframe(None, -1, title="Title Options", size=(520, 390))
frm.Show()
"""Call it here **after** all elements have been created"""
import wx.lib.inspection
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
規則在wxPython中與尺寸儀工作:
- 始終使用
wx.Panel
作爲父母你的控制。無論您的控件本身是否在面板上進行操作,您都可以根據需要將它們堆疊在任意層上。如果你的家長直接到wx.Frame
,你會在Windows上看到醜陋的灰色背景。
- 定義您的控制對象(可以再次面板)
- 選擇合適的定徑機
- 佈局篩分器所有的元素
- 重要:告訴你的主分級機到父元素來定位本身(
main_panel.SetSizer(mymainsizer)
)
- 告訴主面板的面板父的大小以適應(
mymainsizer.Fit(self)
其中篩分器,假設self
是main_panel
父。使用self.SetSize((<w>, <h>))
如果Fit
出錯。
由於它是最完整和最具建設性的答案,因此它已被接受 - 謝謝 - 它工作得非常好。 – chow