1

如何找出事件发生在哪个 AuiNotebook 页面?

编辑:对此感到抱歉。这是一个代码示例。如何从单击鼠标的女巫中找到笔记本页面?

#!/usr/bin/python

#12_aui_notebook1.py

import wx
import wx.lib.inspection

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)

        self.nb = wx.aui.AuiNotebook(self)

        self.new_panel('Pane 1')
        self.new_panel('Pane 2')
        self.new_panel('Pane 3')

    def new_panel(self, nm):
        pnl = wx.Panel(self)
        self.nb.AddPage(pnl, nm)
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.nb, 1, wx.EXPAND)
        self.SetSizer(self.sizer)

        pnl.Bind(wx.EVT_LEFT_DOWN, self.click)

    def click(self, event):
        print 'Mouse click'
            #How can I find out from witch page this click came from?


class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, '12_aui_notebook1.py')
        frame.Show()
        self.SetTopWindow(frame)
        return 1

if __name__ == "__main__":
    app = MyApp(0)
#    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

欧尔扬·彼得森

4

1 回答 1

1

对于鼠标单击,您可以假设当前选定的页面是获得点击的页面。我在您的代码中添加了几行。看评论

def new_panel(self, nm):
    pnl = wx.Panel(self)
    # just to debug, I added a string attribute to the panel
    # don't you love dynamic languages? :)
    pnl.identifierTag = nm   
    self.nb.AddPage(pnl, nm)
    self.sizer = wx.BoxSizer()
    self.sizer.Add(self.nb, 1, wx.EXPAND)
    self.SetSizer(self.sizer)

    pnl.Bind(wx.EVT_LEFT_DOWN, self.click)

def click(self, event):
    print 'Mouse click'
    # get the current selected page
    page = self.nb.GetPage(self.nb.GetSelection())
    # notice that it is the panel that you created in new_panel
    print page.identifierTag
于 2009-02-24T05:02:36.020 回答