2013-03-13 64 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

相關問題