问题
This new question is build on this
I am having problem with managing the layout in wxPython. In this program I have two buttons in two layouts. But no matter what I do I cant change the position or alignment of those buttons. Those buttons got fixed in blue and yellow layout.
My code goes like this:
import wx
class myframe(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, size=(1000,700))
self.TitlePanel = wx.Panel(self, size=(350, 400))
self.TitlePanel.SetBackgroundColour("green")
self.newPanel = wx.Panel(self, size=(300, 250))
self.newPanel.SetBackgroundColour("black")
self.newPanel.Hide()
self.imgPanel = wx.Panel(self, size=(300, 250))
self.imgPanel.SetBackgroundColour("red")
self.modulePanel=wx.Panel(self, size=(350, 250))
self.modulePanel.SetBackgroundColour("blue")
self.TCPanel=wx.Panel(self, size=(300, 250))
self.TCPanel.SetBackgroundColour("yellow")
self.myGridSizer = wx.GridBagSizer(1,1)
self.myGridSizer.SetEmptyCellSize((0, 0))
self.myGridSizer.Add(self.TitlePanel, pos=(0, 0), span=(4,8), flag=wx.EXPAND)
self.myGridSizer.Add(self.imgPanel, pos=(0, 10), span=(4,8), flag=wx.EXPAND)
self.myGridSizer.Add(self.modulePanel, pos=(10, 0), span=(4,8), flag=wx.EXPAND)
self.myGridSizer.Add(self.TCPanel, pos=(10, 10), span=(4,8), flag=wx.EXPAND)
############################# Add Text to 1st Panel #########################################
self.text1 = wx.StaticText(self.TitlePanel, label="This is a test run",style=wx.ALIGN_LEFT,size=(300,-1))
font = wx.Font(18, wx.DECORATIVE, wx.ITALIC,wx.BOLD, wx.NORMAL)
self.text1.SetFont(font)
self.titleSizer = wx.BoxSizer()
self.titleSizer.Add(self.text1, flag=wx.CENTER|wx.LEFT|wx.ALIGN_RIGHT,border=10)
self.TitlePanel.SetSizer(self.titleSizer)
#########################################################################################
############################### Add Buttons in 3rd Panel ################################
self.moduleSizer = wx.BoxSizer()
self.button1 = wx.Button(self.modulePanel, label="Show Yellow Panel",size=(200,-1),style=wx.ALIGN_RIGHT)
self.moduleSizer.Add(self.button1, flag=wx.CENTER|wx.EXPAND|wx.ALIGN_RIGHT,border=10)
#########################################################################################
self.TCSizer=wx.BoxSizer()
self.button2 = wx.Button(self.TCPanel, label="Bring Black Panel",size=(200,-1))
self.TCSizer.Add(self.button2, flag=wx.CENTER|wx.RIGHT|wx.ALIGN_RIGHT,border=10)
#########################################################################################
self.SetSizer(self.myGridSizer)
self.text1.Bind(wx.EVT_LEFT_DCLICK, self.hideMe)
self.button1.Bind(wx.EVT_BUTTON, self.showMe)
self.button2.Bind(wx.EVT_BUTTON, self.hideMe)
def hideMe(self, event):
self.TCPanel.Hide()
self.myGridSizer.Replace(self.TCPanel, self.newPanel)
self.newPanel.Show()
self.Layout()
def showMe(self, event):
self.newPanel.Hide()
self.myGridSizer.Replace(self.newPanel, self.TCPanel)
self.TCPanel.Show()
self.Layout()
if __name__ == "__main__":
app = wx.App()
region = myframe()
region.Show()
app.MainLoop()
Right now if I can take the buttons to slightly right side of the layouts that would be good. And is there any good tutorial for this. Please suggest.
What I wish to do:
Button from blue panel should be at the middle of blue panel
Button from yellow panel will have a indent of my desired pixels. If I wish to leave a gap of 20 pixel then button will be placed after 20 pixels in yellow panel.
If this is possible then it would be a big help.

回答1:
To get this button to be positioned in the centre just use the flag wx.CENTER. You also need to set this sizer to as the panels sizer.
self.moduleSizer = wx.BoxSizer(wx.VERTICAL) self.button1 = wx.Button(self.modulePanel, label="Show Yellow Panel", size=(200, -1)) self.moduleSizer.Add(self.button1, flag=wx.CENTER) self.modulePanel.SetSizer(self.moduleSizer)
To get this button positioned on the left with an indent of 20 use the flag wx.Left and set the border as 20. As above you also need to set this sizer to its panel.
self.TCSizer = wx.BoxSizer(wx.VERTICAL) self.button2 = wx.Button(self.TCPanel, label="Bring Black Panel", size=(200, -1)) self.TCSizer.Add(self.button2, flag=wx.LEFT, border=20) self.TCPanel.SetSizer(self.TCSizer)
At the end of you init method call.
self.Layout()
to get the sizers to update themselves.
来源:https://stackoverflow.com/questions/20696187/layout-management-with-wxpython