2012-03-06 107 views
1
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和面板?wxpython两个面板布局

回答

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() 
+0

**如果您使用sizer,那么您不能提供x/y坐标来定位小部件**这是我没有注意到的问题,谢谢!无论如何,我学习Python与您的博客~~ – shch1289 2012-03-07 03:56:35

+1

添加两种不同的颜色清楚其中的两个面板是^^ 'panel1.SetBackgroundColour(“#6f8089”)' – 2013-11-30 04:55:21

+1

没错,就是检查面板定位的好方法之一。另一个是使用Widget检测工具。 – 2013-12-02 14:43:32