1

当用户选择或更改笔记本上的选项卡时,我正在尝试触发一个事件。这是我的代码。在PageOne 类中,我尝试将wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED 与ChangingTest(self, evt) 函数绑定。似乎无论我在绑定时尝试使用什么事件,我都无法触发事件。这可能吗?我错过了什么?

我点击按钮并调用self.ChangingTest没有问题,但我想点击标签调用self.ChangingTest。

import wx
import wx.lib.inspection

class PageOne(wx.Panel):
   def __init__(self, parent):
       wx.Panel.__init__(self, parent)
       box = wx.BoxSizer(wx.VERTICAL)
       # Want the users' click on the panel tab to fire an event and 
       # call ChangingTest
       self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.ChangingTest)

       cmdClick = wx.Button(self, wx.ID_CLOSE, "Click Me")
       cmdClick.Bind(wx.EVT_BUTTON, self.ChangingTest)
       box.Add(cmdClick, 0, wx.ALL, 10)


   def ChangingTest(self, evt):
       print "ChangingTest2"


class PageTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageTwo object", (40,40))

class PageThree(wx.Panel):
   def __init__(self, parent):
       wx.Panel.__init__(self, parent)
       t = wx.StaticText(self, -1, "This is a PageThree object", (60,60))


class MainFrame(wx.Frame):
   def __init__(self):
       wx.Frame.__init__(self, None, title="Simple Notebook Example")

       # Here we create a panel and a notebook on the panel
       p = wx.Panel(self)
       nb = wx.Notebook(p)

       # create the page windows as children of the notebook
       page1 = PageOne(nb)
       page2 = PageTwo(nb)
       page3 = PageThree(nb)

       # add the pages to the notebook with the label to show on the tab
       nb.AddPage(page1, "Page 1")
       nb.AddPage(page2, "Page 2")
       nb.AddPage(page3, "Page 3")

       # finally, put the notebook in a sizer for the panel to manage
       # the layout
       sizer = wx.BoxSizer()
       sizer.Add(nb, 1, wx.EXPAND)
       p.SetSizer(sizer)



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

我在这里有工作版本:

import wx

class PageOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageOne object", (20,20))

class PageTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageTwo object", (40,40))

class PageThree(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageThree object", (60,60))


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Simple Notebook Example")

        # Here we create a panel and a notebook on the panel
        p = wx.Panel(self)
        nb = wx.Notebook(p)

        # create the page windows as children of the notebook
        page1 = PageOne(nb)
        page2 = PageTwo(nb)
        page3 = PageThree(nb)

        # add the pages to the notebook with the label to show on the tab
        nb.AddPage(page1, "Page 1")
        nb.AddPage(page2, "Page 2")
        nb.AddPage(page3, "Page 3")

        # finally, put the notebook in a sizer for the panel to manage
        # the layout
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

    # bind event to notebook
        nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.ChangingTest)

    def ChangingTest(self, evt):
       print "It worked!"


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

1 回答 1

3

使用 wx.EVT_NOTEBOOK_PAGE_CHANGED 而不是 wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED 因为您使用的是 wx.Notebook,而不是 wx.aui.AuiNotebook。此外,您不应该绑定 PageOne 类,而是绑定 notebook 对象。因此,您可以编写parent.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.ChangingTest)或将其绑定到 PageOne 类 ( nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, ...)) 之外。

于 2013-03-13T23:12:18.167 回答