【问题标题】:Replace an Existing layout with new layout with wxPython使用 wxPython 将现有布局替换为新布局
【发布时间】:2014-01-07 17:38:58
【问题描述】:

我是 wxPython 的新手。我正在使用 Gridbagsizer 进行布局。 我几乎成功地做出了我想要的布局。但是对于一些未知的问题,它给出了一些问题。

我的目标: 我做了5个布局。绿色,红色,蓝色,黄色和黑色。当我双击“This is test run”时,黄色布局应该完全被黑色布局所取代。

实际发生的情况: 黄色被黑色取代。没问题。但由于某种原因,蓝色布局位置移到了底部。

我的代码是这样的:

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

那么,如何替换布局并保持现有布局不变?

【问题讨论】:

  • 请更正源代码的缩进。
  • 我不知道为什么会发生这种情况,我在 Ubuntu 中编写了这段代码,它很好。但是现在当我在 Windows 上运行它时,文本部分给出了一个错误,没有少我编辑了这个,我认为现在这个至少运行成功了。再次感谢您的回答。

标签: python ubuntu layout wxpython


【解决方案1】:

首先,您应该更正您的text1 相对于其位置的控制方式。如果它是TitlePanel 的孩子,那么您应该为TitlePanel 使用新的sizer,并将text1 放入其中。您的 sizer 应该遵循父子层次结构。

接下来,仅仅Hide()Show() 是不够的,你必须正确替换sizer 中的元素。最简单的方法是使用sizer.Replace(old_widget, new_widget)

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

还有一些关于您的代码的注意事项:

  1. 不要用空格包围括号。它是丑陋的,不符合一般的 Python 风格。
  2. 不要将局部变量与小部件的对象属性混合。使用一种方式并坚持下去,最好使用对象属性。这样您就可以通过所有方法访问您的小部件。
  3. 通过属性设置等对小部件创建进行分组通常更具可读性。在代码中很难找到哪个面板是哪种颜色。

【讨论】:

  • 非常感谢您的回答。我一定会按照您的建议工作,谢谢。
  • 请帮我解决这个问题stackoverflow.com/questions/20696187/…
  • 我有一个问题,是否可以通过命令找出哪些布局是活动的,哪些布局是隐藏的?
猜你喜欢
  • 2016-07-08
  • 2011-08-09
  • 2013-08-01
  • 2018-03-16
  • 1970-01-01
  • 2019-12-12
  • 2011-12-01
  • 2014-01-08
  • 2017-11-22
相关资源
最近更新 更多