如何将wx.Panel放在另一个wx.Panel中?只是一个不起作用的小测试程序:
import wx
class TstApp(wx.App):
def OnInit(self):
self.frame = wx.Frame(None, -1, "TstApp", wx.DefaultPosition,
wx.DefaultSize)
self.pn = wx.Panel(self.frame, -1)
self.pn2 = wx.Panel(self.pn, -1)
self.btn = wx.Button(self.pn2, -1, "A Button")
self.frame.Show()
self.SetTopWindow(self.frame)
return True
app = TstApp(0)
app.MainLoop()
答案 0 :(得分:0)
你完全错过了“布局管理”这个主题。我建议你阅读关于zetcode的教程的layout section。我已经介绍了wx.BoxSizer
来修复你的示例代码:
import wx
class TstApp(wx.App):
def OnInit(self):
self.frame = wx.Frame(None, -1, "TstApp", wx.DefaultPosition,
wx.DefaultSize)
self.pn = wx.Panel(self.frame, -1)
sizer1 = wx.BoxSizer(wx.VERTICAL)
self.pn.SetSizer(sizer1)
self.pn2 = wx.Panel(self.pn, -1)
sizer2 = wx.BoxSizer(wx.VERTICAL)
self.pn2.SetSizer(sizer2)
sizer1.Add(self.pn2)
self.btn = wx.Button(self.pn2, -1, "A Button")
sizer2.Add(self.btn)
self.frame.Show()
self.SetTopWindow(self.frame)
return True
app = TstApp(0)
app.MainLoop()
顺便说一句,最好在从wx.Frame
派生的类中创建小部件等。
答案 1 :(得分:0)
正如phineas已经说过的那样,你应该使用sizer来安排你的小部件。但是如果你真的想把多个面板放在一起,那么你也可以使用一个sizer。
import random
import wx
########################################################################
class RandomPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
color = random.choice(["green", "blue", "yellow", "red"])
self.SetBackgroundColour(color)
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
firstSubPanel = RandomPanel(self)
secondSubPanel = RandomPanel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(firstSubPanel, 1, wx.EXPAND|wx.ALL, 5)
sizer.Add(secondSubPanel, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizer(sizer)
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Panels")
panel = MainPanel(self)
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()