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.aui.AuiNotebook ではなく wx.Notebook を使用しているため、wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED の代わりに wx.EVT_NOTEBOOK_PAGE_CHANGED を使用してください。また、PageOne クラスではなく、ノートブック オブジェクトをバインドする必要があります。parent.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.ChangingTest)そのため、PageOne クラス ( ) の外で記述またはバインドすることができますnb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, ...)

于 2013-03-13T23:12:18.167 に答える