wxpython两个面板布局

时间:2012-03-06 04:45:55

标签: wxpython

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title,size=(250, 250))

        panel1 = wx.Panel(self, -1,pos=(0,100),size=(100,100))
        button1 = wx.Button(panel1, -1, label="click me")

        panel2 = wx.Panel(self, -1,pos=(0,200))
        button2 = wx.Button(panel2, -1, label="click me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel1,0,wx.EXPAND|wx.ALL,border=10)
        sizer.Add(panel2,0,wx.EXPAND|wx.ALL,border=10)

        self.SetSizer(sizer)



class MyApp(wx.App):
     def OnInit(self):
         frame = MyFrame(None, -1, 'frame')
         frame.Show(True)
         return True

app = MyApp(0)
app.MainLoop()

我想在wxpython中测试两个面板布局,我改变了pos(x,y),但它不起作用。所以 如何布局只使用boxsizer和面板?

1 个答案:

答案 0 :(得分:2)

我不确定你在问什么。如果使用sizer,则无法提供x / y坐标来定位窗口小部件。如果你只是想知道为什么面板看起来很奇怪,那是因为你下面没有正常的面板。下面的代码是解决这个问题的一种方法。另一种方法是在将每个面板添加到BoxSizer时给出比例大于零的每个面板。

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title,size=(250, 250))

        topPanel = wx.Panel(self)

        panel1 = wx.Panel(topPanel, -1,pos=(0,100),size=(100,100))
        button1 = wx.Button(panel1, -1, label="click me")

        panel2 = wx.Panel(topPanel, -1,pos=(0,200))
        button2 = wx.Button(panel2, -1, label="click me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel1,0,wx.EXPAND|wx.ALL,border=10)
        sizer.Add(panel2,0,wx.EXPAND|wx.ALL,border=10)

        topPanel.SetSizer(sizer)



class MyApp(wx.App):
     def OnInit(self):
         frame = MyFrame(None, -1, 'frame')
         frame.Show(True)
         return True

app = MyApp(0)
app.MainLoop()