Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 138529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:16:43+00:00 2026-05-11T07:16:43+00:00

I am new to Python. I am writing an application using wxPython and I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:16:43+00:00Added an answer on May 11, 2026 at 7:16 am

    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…

    class Toolbar():   def __init__(self, frame_to_connect_to):     frame_to_connect_to.toolbar = frame_to_connect_to.CreateToolBar(style=(wx.TB_HORZ_LAYOUT | wx.TB_TEXT))     frame_to_connect_to.toolbar.SetToolBitmapSize((32,32))     frame_to_connect_to.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/fileopen.png'))     frame_to_connect_to.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/filesave.png'))     frame_to_connect_to.toolbar.AddSeparator()     frame_to_connect_to.toolbar.Realize() 

    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:

    class Window(wx.Frame)   def __init__(self, parent, plot):     wx.Frame.__init__(self, parent, wx.ID_ANY, 'Name', size =(900, 600))     self.Centre()     self._init_toolbar()    def _init_toolbar(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() 

    You get all the benefits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Log stack traces to a log file they wont mean… May 11, 2026 at 4:56 pm
  • Editorial Team
    Editorial Team added an answer I suggest to have a look at existing APIs. You… May 11, 2026 at 4:56 pm
  • Editorial Team
    Editorial Team added an answer You can do that with JavaScript by adding an event… May 11, 2026 at 4:56 pm

Related Questions

I'm writing an application in Python (2.6) that requires me to use a dictionary
I'm coding the menu for an application I'm writing in python, using wxPython libraries
Alright, this is probably a really silly question but I am new to Python/Django
I have written a script that goes through a bunch of files and snips

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.