2013-03-13 29 views
1

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

我没有问题点击按钮并调用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() 

回答

3

使用wx.EVT_NOTEBOOK_PAGE_CHANGED代替wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED因为您使用wx.Notebook,不wx.aui.AuiNotebook。此外,您应该绑定不PageOne类,但笔记本对象。所以你可以写parent.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.ChangingTest)或者你可以将它绑定到PageOne类(nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, ...))之外。

+0

感谢您的信息。我将事件绑定到错误的对象。一旦我在笔记本上做了“绑定”而不是PageOne,它工作得很好。再次感谢你的帮助。 – 2013-03-15 18:24:22

+0

这是代码的工作版本。 级的主机(wx.Frame): DEF __init __(个体): wx.Frame .__初始化__(个体,无,标题= “简单实施例笔记本”) #这里我们创建一个面板和一个笔记本面板 上p = wx.Panel(个体经营) NB = wx.Notebook(p) #创建页面窗口作为笔记本电脑的孩子 第1页= PAGEONE(NB) ... #绑定事件笔记本 nb.Bind (wx.EVT_NOTEBOOK_PAGE_CHANGED,self.ChangingTest) def ChangingTest(self,evt): print“It worked!” – 2013-03-15 18:56:02

相关问题