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

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
asked Mar 6, 2012 at 4:45

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

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')
Yes, that's one good way to check panel positioning. Another is to use the Widget Inspection Tool.
You are missing app.MainLoop() at the end of your code :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.