1

Ive managed to make a small example of what I would like to achieve. When I double click on the green panel, it dissapears which is what I want and when I double click on the red panel the green panel appears again which is also fine. What I am having trouble with is how do I move the green panel up to take the place of the red panel when the red panel is not shown and remove the green panel back to how it was when the red panel appears, without having to change the position of them in the grid sizer.

Can someone please help?

Below is the template code which I am trying to achieve this:

import wx
class myframe(wx.Frame):
    def __init__(self):
        "Constructor. No arguments"
        wx.Frame.__init__(self, None, size=(2000,2000))
        self.myPanel1 = wx.Panel( self, size=(300, 300) )
        myPanel2 = wx.Panel( self, size=(300, 300) )

        self.myPanel1.SetBackgroundColour("green")
        myPanel2.SetBackgroundColour("red")
        myGridSizer = wx.GridBagSizer()
        myGridSizer.Add(self.myPanel1, pos=(0, 0), span=(1,1), flag=wx.EXPAND)
        myGridSizer.Add(myPanel2, pos=(1, 0), span=(1,1), flag=wx.EXPAND)
        self.SetSizer(myGridSizer)
        self.myPanel1.Bind(wx.EVT_LEFT_DCLICK, self.hideMe)
        myPanel2.Bind(wx.EVT_LEFT_DCLICK, self.showMe)

    def hideMe(self, event):
        print "hide!"
        self.myPanel1.Hide()

    def showMe(self, event):
        print "show!"
        self.myPanel1.Show()

if __name__ == "__main__":
    print "Running Demo"
    app = wx.App()
    region = myframe()
    region.Show()
    app.MainLoop()
4

2 回答 2

1

从技术上讲,如果不实际移动小部件,您将无法使其以这种方式工作。在 Yoriz 的示例中,它几乎将红色面板放在绿色面板所在的位置,但您会注意到有一条灰色条带将红色面板与框架顶部隔开。那是因为网格尺寸器仍在为隐藏的绿色面板保留空间。

如果你希望它是无缝的,你会想看看 sizer 的 Detach() 和各种 Insert() 方法:http ://wxpython.org/docs/api/wx.Sizer-class.html

于 2013-06-13T18:52:42.990 回答
0

调用 Hide and Show 后调用 self.Layout()。我添加了 'myGridSizer.SetEmptyCellSize((0, 0))' 以消除顶部的间隙。

import wx
class myframe(wx.Frame):
    def __init__(self):
        "Constructor. No arguments"
        wx.Frame.__init__(self, None, size=(2000,2000))
        self.myPanel1 = wx.Panel( self, size=(300, 300) )
        myPanel2 = wx.Panel( self, size=(300, 300) )

        self.myPanel1.SetBackgroundColour("green")
        myPanel2.SetBackgroundColour("red")
        myGridSizer = wx.GridBagSizer()
        myGridSizer.Add(self.myPanel1, pos=(0, 0), span=(1,1), flag=wx.EXPAND)
        myGridSizer.Add(myPanel2, pos=(1, 0), span=(1,1), flag=wx.EXPAND)
        self.SetSizer(myGridSizer)
        self.myPanel1.Bind(wx.EVT_LEFT_DCLICK, self.hideMe)
        myPanel2.Bind(wx.EVT_LEFT_DCLICK, self.showMe)
        myGridSizer.SetEmptyCellSize((0, 0))

    def hideMe(self, event):
        print "hide!"
        self.myPanel1.Hide()
        self.Layout()

    def showMe(self, event):
        print "show!"
        self.myPanel1.Show()
        self.Layout()

if __name__ == "__main__":
    print "Running Demo"
    app = wx.App()
    region = myframe()
    region.Show()
    app.MainLoop()
于 2013-06-13T18:15:32.433 回答