1

我想问是否可以在 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()

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

4

1 回答 1

2

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

  • 带有按钮和事件的面板
  • 主机中的按钮调用功能
  • 大型机将下一个面板(带网格)添加到 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()
于 2013-10-29T14:00:01.257 回答