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()
I want to test two panel layout in wxpython , i change the pos(x,y) but it does't work . so how to layout just use boxsizer and panel ?
Mike Driscoll
33.2k9 gold badges51 silver badges91 bronze badges
1 Answer 1
I'm not sure what you're asking. If you use a sizer, then you cannot provide x/y coordinates to position the widgets. If you're just wondering why the panels look weird, it's because you don't have a normal panel underneath them. The code below is one way to fix that. The other way would be to give each of the panels you had a proportion greater than zero when adding them to the 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()
answered Mar 6, 2012 at 14:27
Mike Driscoll
33.2k9 gold badges51 silver badges91 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
shch1289
If you use a sizer, then you cannot provide x/y coordinates to position the widgets this is the problem i didn't notice , thanks! Anyway , i learn python with your blog ~~
おおさま
Add two different colors makes it clear where the two panels are ^^
panel1.SetBackgroundColour('#6f8089')Mike Driscoll
Yes, that's one good way to check panel positioning. Another is to use the Widget Inspection Tool.
Mario S. E.
You are missing
app.MainLoop() at the end of your code :)lang-py