Reputation: 43
I am trying to create two tabs using wxPython's wx.Notebook
; however I am having difficulty to understand where I should put the Bind()
if the button and the ListBox are in different tabs.
Here is how my code looks like: It has class TabOne
, and TabTwo
, both of wx.Panel
. The class enrollment
is a wx.Frame
that uses TabOne
and TabTwo
as panels.
class TabOne(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent)
btn = wx.Button(self, 1, "Add", (120,110))
class TabTwo(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent)
lst = wx.ListBox(self, 1, (40,40))
class enrollment(wx.Frame):
def __init__(self, *args, **kargs):
super(enrollment, self).__init__(*args, **kargs)
panel = wx.Panel(self)
nb = wx.Notebook(panel)
tab1 = TabOne(nb)
tab2 = TabTwo(nb)
nb.AddPage(tab1, "Button")
nb.AddPage(tab2, "ListBox")
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
panel.SetSizer(sizer)
self.Show(True)
def main():
app = wx.App()
enrollment(None)
app.MainLoop()
if __name__ = '__main__':
main()
Just like to know if I want to add a Bind()
for btn
in TabOne
in order to it to update the ListBox
in TabTwo
, where should I add the definition for it? I am at a loss at the moment. Thanks Guys.
Upvotes: 0
Views: 325
Reputation: 2034
There are different ways that you can organize it. The binding of a button is one thing and the action you take inside the handler is another. My approach would be to let the enrollment
class handle binding and the action because it knows about both of the tabs. You would not want to create a dependency between TabOne and TabTwo.
class TabOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.btn = wx.Button(self, 1, "Add", (120,110))
def BindButton(self, handler):
# let users of TabOne to register their button handler
self.btn.Bind(wx.EVT_BUTTON, handler)
class TabTwo(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent)
self.lst = wx.ListBox(self, 1, (40,40))
def InformButtonClicked(self):
# signal to TabTwo about the button click
self.lst.Append("Item")
class enrollment(wx.Frame):
def __init__(self, *args, **kargs):
super(enrollment, self).__init__(*args, **kargs)
panel = wx.Panel(self)
nb = wx.Notebook(panel)
self.tab1 = TabOne(nb)
self.tab2 = TabTwo(nb)
# register your button handler
self.tab1.BindButton(self.button_handler)
nb.AddPage(self.tab1, "Button")
nb.AddPage(self.tab2, "ListBox")
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
panel.SetSizer(sizer)
self.Show(True)
def button_handler(self, evt):
# this method gets called when the button is clicked
self.tab2.InformButtonClicked()
Upvotes: 1