如何在wxpython中添加带事件按钮的面板?

时间:2013-10-29 10:39:01

标签: python wxpython

我想问一下,是否可以在wxpython中添加带有事件按钮的wx.Panel?有很多例子如何切换面板隐藏第一个并显示第二个,但它们对我来说没用。我想用添加按钮创建面板。例如,我有这样的面板

import wx
import wx.grid as grid

class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent = parent)


class SecondPanel(wx.Panel):
    def __init__(self, parent,a,b):
        wx.Panel.__init__(self, parent=parent)
        MyGrid=grid.Grid(self)
        MyGrid.CreateGrid(a, b)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(MyGrid, 0, wx.EXPAND)
        self.SetSizer(sizer)

class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="test",
                          size=(800,600))
        self.splitter = wx.SplitterWindow(self)

        self.panelOne = MainPanel(self.splitter)


        self.panelTwo = SecondPanel(self.splitter, 1, 1)
        txtOne = wx.StaticText(self.panelOne, -1, label = "piradoba", pos = (20,10))
        self.txtTwo = wx.StaticText(self.panelOne, -1, label = "", pos = (40,80))
        self.txtPlace = wx.TextCtrl(self.panelOne, pos = (20,30))
        button = wx.Button(self.panelOne, label = "search", pos = (40,100))
        button.Bind(wx.EVT_BUTTON, self.Onbutton)
        self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
        self.splitter.SetMinimumPaneSize(20)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.splitter, 1, wx.EXPAND)
        self.SetSizer(sizer)
   def Onbutton(self, event):
        var=self.txtPlace.GetValue()
        if len(var) == 9 or len(var) == 11:
           ???????????????????????????????????????????????

if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()

例如现在我想在此事件中添加新面板我该怎么办?我想用事件创建这个面板。

1 个答案:

答案 0 :(得分:1)

我不知道你是否需要它,但在这个例子中你有:

  • 带按钮和事件的面板
  • 大型机中的按钮调用功能
  • 主机将下一个面板(带网格)添加到boxsizer

在Linux Mint + Python 2.7.4上测试

import wx
import wx.grid as grid

class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent = parent)

        self.txtOne = wx.StaticText(self, -1, label = "piradoba", pos = (20,10))
        self.txtPlace = wx.TextCtrl(self, pos = (20,30))
        self.txtTwo = wx.StaticText(self, -1, label = "", pos = (20,40))

        button = wx.Button(self, label = "search", pos = (20,70))
        button.Bind(wx.EVT_BUTTON, self.onButton)

    def onButton(self, event):
        var=self.txtPlace.GetValue()
        if len(var) == 9 or len(var) == 11:
            print "???"
        # MainPanel->SplitterWindow->MainFrame ( 2x GetParent() )
        self.GetParent().GetParent().AddPanel()

class SecondPanel(wx.Panel):

    def __init__(self, parent,a,b):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)

        MyGrid=grid.Grid(self)
        MyGrid.CreateGrid(a, b)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(MyGrid, 0, wx.EXPAND)
        self.SetSizer(sizer)

class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="test", size=(800,600))

        self.splitter = wx.SplitterWindow(self)

        self.panelOne = MainPanel(self.splitter)
        self.panelTwo = SecondPanel(self.splitter, 1, 1)

        self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
        self.splitter.SetMinimumPaneSize(20)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.splitter, 2, wx.EXPAND)

        self.SetSizer(self.sizer)

    def AddPanel(self):
        self.newPanel = SecondPanel(self, 1, 1)
        self.sizer.Add(self.newPanel, 1, wx.EXPAND)
        self.sizer.Layout()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()