我试图将简单书的.ChangeSelection()函数绑定到不在书中但在同一帧中的按钮。
当前形式的示例代码是一个简单列出的框架,在页面的左侧有按钮,而书本则在右侧显示。目前LHS按钮尚未绑定,RHS wx.simplebook显示空白页“ 0”。
我想让按钮在按下时显示相应的页面,但是正在努力寻找合适的示例简单手册以从中学习这种格式的示例。
import wx
### SIMPLEBOOK TEST
class MainPage (wx.Frame):
def __init__ (self,parent):
wx.Frame.__init__ ( self, None, 1, title = "NOTEBOOK", pos= wx.DefaultPosition, size = wx.Size( 320,400 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
buttonSizer = wx.BoxSizer(wx.VERTICAL)
pagesSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(buttonSizer,1,wx.EXPAND,0)
mainSizer.Add(pagesSizer,1,wx.EXPAND,0)
page1Button = wx.Button(self,wx.ID_ANY,"Page 1",wx.DefaultPosition,wx.DefaultSize,0)
page2Button = wx.Button(self,wx.ID_ANY,"Page 2",wx.DefaultPosition,wx.DefaultSize,0)
buttonSizer.Add(page1Button,1,wx.ALL|wx.EXPAND,0)
buttonSizer.Add(page2Button,1,wx.ALL|wx.EXPAND,0)
book = BookTest(self)
pagesSizer.Add(book,1,wx.EXPAND|wx.ALL,2)
self.SetSizer(mainSizer)
self.Layout()
###########################################################
# My attempts to bind the page selection to the buttons.
#page1Button.Bind(wx.EVT_BUTTON,BookTest.page1sel())
#page2Button.Bind(wx.EVT_BUTTON,page2sel())
###########################################################
class BookTest (wx.Simplebook):
def __init__(self, parent):
wx.Simplebook.__init__(self, parent=parent)
blankpage=Blankpage(self)
page1=Page1(self)
page2=Page2(self)
self.AddPage(blankpage,"")
self.AddPage(page1,"")
self.AddPage(page2,"")
#### Here are the attempts to change page, both in a nested def and standalone.
def page1sel():
self.ChangeSelection(1)
def page2sel():
BookTest.ChangeSelection(2)
class Blankpage (wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self,parent=parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.Show()
class Page1 (wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self,parent=parent)
sizer = wx.BoxSizer(wx.VERTICAL)
t=wx.StaticText(self,-1,"THIS IS PAGE 1")
sizer.Add(t,1,wx.EXPAND,0)
self.Show()
class Page2 (wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent=parent)
sizer = wx.BoxSizer(wx.VERTICAL)
t=wx.StaticText(self,-1,"THIS IS PAGE 2")
sizer.Add(t,1,wx.EXPAND,0)
if __name__ == '__main__':
app = wx.App()
frm = MainPage(None)
frm.Centre()
frm.Show()
app.MainLoop()
谢谢,日落
答案 0 :(得分:1)
您真的很亲近。仅缺少两个细节。
1-您通过在page1sel
中创建的类的实例,从class BookTest
调用方法class MainPage
。这意味着像这样绑定:
page1Button.Bind(wx.EVT_BUTTON,book.page1sel)
2-方法page1sel
需要从event
接收Bind
。因此,必须将其定义为:
def page1sel(self, event):
page2sel
也是(我认为)应该是class BookTest
的一种方法。
代码:
import wx
### SIMPLEBOOK TEST
class MainPage (wx.Frame):
def __init__ (self,parent):
wx.Frame.__init__ ( self, None, 1, title = "NOTEBOOK", pos= wx.DefaultPosition, size = wx.Size( 320,400 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
buttonSizer = wx.BoxSizer(wx.VERTICAL)
pagesSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(buttonSizer,1,wx.EXPAND,0)
mainSizer.Add(pagesSizer,1,wx.EXPAND,0)
page1Button = wx.Button(self,wx.ID_ANY,"Page 1",wx.DefaultPosition,wx.DefaultSize,0)
page2Button = wx.Button(self,wx.ID_ANY,"Page 2",wx.DefaultPosition,wx.DefaultSize,0)
buttonSizer.Add(page1Button,1,wx.ALL|wx.EXPAND,0)
buttonSizer.Add(page2Button,1,wx.ALL|wx.EXPAND,0)
book = BookTest(self)
pagesSizer.Add(book,1,wx.EXPAND|wx.ALL,2)
self.SetSizer(mainSizer)
self.Layout()
###########################################################
# My attempts to bind the page selection to the buttons.
page1Button.Bind(wx.EVT_BUTTON,book.page1sel)
page2Button.Bind(wx.EVT_BUTTON,book.page2sel)
###########################################################
class BookTest (wx.Simplebook):
def __init__(self, parent):
wx.Simplebook.__init__(self, parent=parent)
blankpage=Blankpage(self)
page1=Page1(self)
page2=Page2(self)
self.AddPage(blankpage,"")
self.AddPage(page1,"")
self.AddPage(page2,"")
#### Here are the attempts to change page, both in a nested def and standalone.
def page1sel(self, event):
self.ChangeSelection(1)
def page2sel(self, event):
self.ChangeSelection(2)
class Blankpage (wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self,parent=parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.Show()
class Page1 (wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self,parent=parent)
sizer = wx.BoxSizer(wx.VERTICAL)
t=wx.StaticText(self,-1,"THIS IS PAGE 1")
sizer.Add(t,1,wx.EXPAND,0)
self.Show()
class Page2 (wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent=parent)
sizer = wx.BoxSizer(wx.VERTICAL)
t=wx.StaticText(self,-1,"THIS IS PAGE 2")
sizer.Add(t,1,wx.EXPAND,0)
if __name__ == '__main__':
app = wx.App()
frm = MainPage(None)
frm.Centre()
frm.Show()
app.MainLoop()