我正在尝试在用户选择或更改笔记本上的标签时触发事件。这是我的代码。在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()
答案 0 :(得分: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, ...)
)之外。