用wxPython的新布局替换现有布局
我是wxPython的新手。我正在用Gridbagsizer做一个布局。 我几乎成功地做出了我想要的布局。但对于一些未知的问题,它给出了一些问题 我的目标: 我做了5个布局。绿色、红色、蓝色、黄色和黑色。当我双击“这是测试运行”时,黄色布局应该被黑色布局完全替换 实际发生的情况: 黄色被黑色代替。没问题。但由于某种原因,蓝色布局的位置会移到底部 我的代码是这样的:用wxPython的新布局替换现有布局,python,ubuntu,layout,wxpython,Python,Ubuntu,Layout,Wxpython,我是wxPython的新手。我正在用Gridbagsizer做一个布局。 我几乎成功地做出了我想要的布局。但对于一些未知的问题,它给出了一些问题 我的目标: 我做了5个布局。绿色、红色、蓝色、黄色和黑色。当我双击“这是测试运行”时,黄色布局应该被黑色布局完全替换 实际发生的情况: 黄色被黑色代替。没问题。但由于某种原因,蓝色布局的位置会移到底部 我的代码是这样的: import wx class myframe(wx.Frame): def __init__(self):
import wx
class myframe(wx.Frame):
def __init__(self):
"Constructor. No arguments"
wx.Frame.__init__(self, None, size=(1000,700))
self.TitlePanel = wx.Panel( self, size=(350, 400) )
self.newPanel = wx.Panel( self, size=(300, 250) )
imgPanel = wx.Panel( self, size=(300, 250) )
modulePanel=wx.Panel( self, size=(350, 250) )
self.TCPanel=wx.Panel( self, size=(300, 250) )
############################################
self.TitlePanel.SetBackgroundColour("green")
imgPanel.SetBackgroundColour("red")
modulePanel.SetBackgroundColour("blue")
self.TCPanel.SetBackgroundColour("yellow")
self.newPanel.SetBackgroundColour("black")
self.newPanel.Hide()
############################################
self.myGridSizer = wx.GridBagSizer(1,1)
self.myGridSizer.Add(self.TitlePanel, pos=(0, 0), span=(4,8), flag=wx.EXPAND)
self.myGridSizer.Add(imgPanel, pos=(0, 10), span=(4,8), flag=wx.ALL)
self.myGridSizer.Add(modulePanel, pos=(10, 0), span=(1,8), flag=wx.ALL)
self.myGridSizer.Add(self.TCPanel, pos=(10, 10), span=(4,8), flag=wx.ALL)
#############################################
self.text1 = wx.StaticText(self.TitlePanel, label="This is a test run",style=2,size=(350,-1))
font = wx.Font(18, wx.DECORATIVE, wx.ITALIC,wx.BOLD, wx.NORMAL)
self.text1.SetFont(font)
#############################################
self.SetSizer(self.myGridSizer)
self.text1.Bind(wx.EVT_LEFT_DCLICK, self.hideMe)
imgPanel.Bind(wx.EVT_LEFT_DCLICK, self.showMe)
self.myGridSizer.SetEmptyCellSize((0, 0))
def hideMe(self, event):
self.TCPanel.Hide()
self.myGridSizer.Add(self.newPanel, pos=(5, 10), span=(4,8), flag=wx.ALL)
self.newPanel.Show()
self.Layout()
def showMe(self, event):
print "show!"
self.newPanel.Hide()
self.TCPanel.Show()
self.Layout()
if __name__ == "__main__":
app = wx.App()
region = myframe()
region.Show()
app.MainLoop()
那么,如何替换布局并保持现有布局不变
首先,您应该纠正
text1
相对于其位置的控制方式。如果它是TitlePanel
的子级,则应为TitlePanel
使用新的大小调整器,并将text1
放入其中。您的大小调整器应遵循父子层次结构
接下来,仅仅Hide()
和Show()
是不够的,您必须正确地替换sizer中的元素。最简单的方法是使用sizer.Replace(旧的\u小部件,新的\u小部件)
还有一些关于代码的注释:
请更正源代码的缩进。我不知道为什么会发生这种情况,我在Ubuntu中编写了这段代码,很好。。但是现在我在Windows上运行这个,文本部分给出了一个错误,尽管我编辑了这个,我认为现在这个至少运行成功了。再次感谢您的回答。非常感谢您的回答。我一定会按照您的建议工作。谢谢。请帮助我。我有一个问题,是否可以通过命令找出哪些版面处于活动状态,哪些版面处于隐藏状态?
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.ALL)
self.myGridSizer.Add(self.modulePanel, pos=(10, 0), span=(1,8), flag=wx.ALL)
self.myGridSizer.Add(self.TCPanel, pos=(10, 10), span=(4,8), flag=wx.ALL)
self.text1 = wx.StaticText(self.TitlePanel, label="This is a test run",style=2,size=(350,-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.TOP|wx.LEFT|wx.ALIGN_RIGHT,border=10)
self.TitlePanel.SetSizer(self.titleSizer)
self.SetSizer(self.myGridSizer)
self.text1.Bind(wx.EVT_LEFT_DCLICK, self.hideMe)
self.imgPanel.Bind(wx.EVT_LEFT_DCLICK, self.showMe)
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()