I am new to Python. I am writing an application using wxPython and I currently my code that generates a toolbar looks like this:
class Window(wx.Frame)
def __init__(self, parent, plot):
wx.Frame.__init__(self, parent, wx.ID_ANY, "Name", size =(900, 600))
self.Centre()
self.toolbar = self.CreateToolBar(style=(wx.TB_HORZ_LAYOUT | wx.TB_TEXT))
self.toolbar.SetToolBitmapSize((32,32))
self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/fileopen.png'))
self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/filesave.png'))
self.toolbar.AddSeparator()
self.toolbar.Realize()
I am trying to clean up the code a bit and I want the toolbar to have its own class so when I want to create a toolbar, I simply call it something like this:
toolbar = Toolbar()
My question is how can I rewrite it so it works like that? Currently my code looks like this:
class Toolbar():
def __init__(self):
self.toolbar = self.CreateToolBar(style=(wx.TB_HORZ_LAYOUT | wx.TB_TEXT))
self.toolbar.SetToolBitmapSize((32,32))
self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/fileopen.png'))
self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/filesave.png'))
self.toolbar.AddSeparator()
self.toolbar.Realize()
I am not quite sure how 'self' works. Do I need to rewrite the init function? How do I fix it? Any help is greatly appreciated. Thanks
Instead of a class that sets up your toolbar, use a function. The function can be a member function of your Window that subclasses wx.Frame. That way, the toolbar will get Created from the correct window, and be attached the way you would expect.
The class that you're writing above would work, if it knew which wx.Frame (your class called Window) to connect the toolbar to. To get it to work you would have to pass the frame object to the toolbar creator class...
It looks like a quick fix... but really using a class to do this is not a good use of classes. (I'd even go so far as to say it was incorrect.)
Really, what would clean things up a bit would be just to move the toolbar stuff to its own member function:
You get all the benefits.