DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. wxPython: Working With Status Bars

wxPython: Working With Status Bars

In wxPython, you can add a status bar to your frame by using the wx.StatusBar class. In this article, we will learn all about how to use status bars in wxPython.

Mike Driscoll user avatar by
Mike Driscoll
·
Jun. 07, 17 · Tutorial
Like (3)
Save
Tweet
Share
6.84K Views

Join the DZone community and get the full member experience.

Join For Free

most applications come with a status bar. the status bar is the widget along the bottom of most applications that you use every day. they give you information about what line you’re editing in a text editor or when you last saved. in wxpython, you can add a status bar to your frame by using the wx.statusbar class. in this article, we will learn all about how to use status bars in wxpython.

no status bars

it’s always good to start at the beginning, so we will begin our journey by looking at some sample code that shows what a frame looks like without a status bar:

import wx
 
class mainframe(wx.frame):
 
    def __init__(self):
        wx.frame.__init__(self, none, title='no statusbars')
 
        panel = wx.panel(self)
 
        self.show()
 
if __name__ == '__main__':
    app = wx.app(false)
    frame = mainframe()
    app.mainloop()

when you run this code, you should see something like the following:

well, that was pretty simple. let’s find out how to add a status bar!

adding a status bar

of course not adding something was pretty simple. but you will soon find that adding a simple one-field status bar is also really easy in wxpython. in fact, it’s really just a one-line change! however, to make it a bit more interesting, we will also set the status bar’s text to something, too. let’s take a look:

import wx
 
class mainframe(wx.frame):
 
    def __init__(self):
        wx.frame.__init__(self, none, title='')
        panel = wx.panel(self)
 
        self.statusbar = self.createstatusbar(1)
        self.statusbar.setstatustext('this goes in your statusbar')
 
        self.show()
 
if __name__ == '__main__':
    app = wx.app(false)
    frame = mainframe()
    app.mainloop()

i am going to run this code on windows 7, as i think windows has one of the easiest-to-see status bars. when you run it, you should see something similar to this:

you will notice that when we created the status bar, we had to call the frame’s createstatusbar() method. the parameter we passed in told the status bar that we only wanted one field in the status bar.

creating a multi-field status bar

a lot of applications can display multiple pieces of information to the user in the application’s status bar. microsoft’s word is a good example as it will list out page information, word count and more in separate sections of the status bar. you can show this kind of thing with wxpython too. let’s see an example:

import wx
 
 
class mainframe(wx.frame):
 
    def __init__(self):
        wx.frame.__init__(self, none, title='statusbars')
        panel = wx.panel(self)
 
        self.statusbar = self.createstatusbar(2)
        self.statusbar.setstatustext('this goes field one')
        self.statusbar.setstatustext('field 2 here!', 1)
 
        self.show()
 
if __name__ == '__main__':
    app = wx.app(false)
    frame = mainframe()
    app.mainloop()

in this example, we pass the number two to createstatusbar to create two equally sized sections in our status bar. then we call setstatustext() . you will note that the first call doesn’t specify which section to put its text. that’s because the default is zero. to put text in a field other than zero, we need to be more explicit. thus, in the second call to setstatustext() , we pass it a one (1) which tells wxpython to put the text in the second section of the status bar.

changing section widths

you can specify the section widths via the status bar’s setstatuswidths() method, which accepts a python list. you can either set fixed width or variable widths in your status bar. for fixed, you just pass a python list of integers where each integer represents the field’s size in pixels. if you’d rather do a variable width, then you will use a list that contains negative numbers. for example, if you had [-2, -1], the first field would take up 66% of the space while the second took up the remaining 33%. you can also mix fixed and variable, like this: [-2, -1, 50]. in this case, you are telling wxpython to take up 66% of the remaining space in the first field, 33% of the remaining space in the second field and 50 pixels in the 3rd field.

this may be easier to see visually, so let’s look at an example!

import wx
 
 
class mainframe(wx.frame):
 
    def __init__(self):
        wx.frame.__init__(self, none, title='statusbars')
        panel = wx.panel(self)
 
        self.statusbar = self.createstatusbar(2)
        self.statusbar.setstatuswidths([100, 300])
        self.statusbar.setstatustext('this goes field one')
        self.statusbar.setstatustext('field 2 here!', 1)
 
        self.show()
 
if __name__ == '__main__':
    app = wx.app(false)
    frame = mainframe()
    app.mainloop()

in this example, we want our first field to be 100 pixels wide with the second field being 300 pixels. when you run the code, you should see something like this:

note that i resized the window to demonstrate that when the frame is wider than 400 pixels, the status bar looks kind of weird. this is a common problem when using fixed widths. you will have similar problems if you use absolute positioning of widgets instead of using sizers. let’s see if we can fix the issue by using a mix of fixed width and variable widths. here’s the change to the code:

import wx
 
 
class mainframe(wx.frame):
 
    def __init__(self):
        wx.frame.__init__(self, none, title='statusbars')
        panel = wx.panel(self)
 
        self.statusbar = self.createstatusbar(2)
        self.statusbar.setstatuswidths([100, -1])
        self.statusbar.setstatustext('this goes field one')
        self.statusbar.setstatustext('field 2 here!', 1)
 
        self.show()
 
if __name__ == '__main__':
    app = wx.app(false)
    frame = mainframe()
    app.mainloop()

you will note that we replaced the 300 pixels with a -1, which means the second field should take up all the space following the first 100 pixels. here’s a screenshot:

this time the status bar doesn’t look so odd. you can resize the status bar as wide as you want now.

getting the status

you can also get the status bar’s status using the statusbar’s getstatustext() method. let’s take a look:

import wx
 
 
class mainframe(wx.frame):
 
    def __init__(self):
        wx.frame.__init__(self, none, title='statusbars')
        panel = wx.panel(self)
 
        status_btn = wx.button(panel, label='get status')
        status_btn.bind(wx.evt_button, self.on_status)
 
        self.statusbar = self.createstatusbar(2)
        self.statusbar.setstatustext('this goes in field one')
        self.statusbar.setstatustext('field 2 here!', 1)
 
        self.show()
 
    def on_status(self, event):
        print self.statusbar.getstatustext()
        print self.statusbar.getstatustext(1)
 
if __name__ == '__main__':
    app = wx.app(false)
    frame = mainframe()
    app.mainloop()

you will note that to get the second field’s text, we had to tell getstatustext explicitly by passing it a one.

changing the status text

we’ve already looked at changing the status bar’s text with setstatustext() . however, there are two more methods worth looking at: pushstatustext() and popstatustext() . these use a stack so that when you call pushstatustext() , it puts the current status into memory on a stack and displays the string that you passed to it. when you call popstatustext() , it will restore the previously stored text. however, if you call setstatustext() between the push and the pop, then the memory will be erased and pop will not restore the status string.

let’s look at an example:

import wx
 
 
class mainframe(wx.frame):
 
    def __init__(self):
        wx.frame.__init__(self, none, title='statusbars')
        panel = wx.panel(self)
 
        status_btn = wx.button(panel, label='set temp status')
        status_btn.bind(wx.evt_button, self.set_temp_status)
 
        restore_btn = wx.button(panel, label='restore status')
        restore_btn.bind(wx.evt_button, self.restore_status)
 
        sizer = wx.boxsizer(wx.horizontal)
        sizer.add(status_btn, 0, wx.all, 5)
        sizer.add(restore_btn, 0, wx.all, 5)
        panel.setsizer(sizer)
 
        self.statusbar = self.createstatusbar(2)
        self.statusbar.setstatustext('this goes in field one')
        self.statusbar.setstatustext('field 2 here!', 1)
 
        self.show()
 
    def set_temp_status(self, event):
        self.statusbar.pushstatustext('this is a temporary status')
 
    def restore_status(self, event):
        self.statusbar.popstatustext()
 
if __name__ == '__main__':
    app = wx.app(false)
    frame = mainframe()
    app.mainloop()

note that pushstatustext() and popstatustext() default to pushing and popping the first field. you will need to specify a different field to push and pop if you need to do that. give this code a try and see what happens!

wrapping up

this article covered a lot of material. you learned how to create a statusbar widget for your frame. you learned how to split up the status bar into multiple fields of multiple widths. you also learned how to get the text from it and change the text too. i will also note that when you create a menubar, you can set each menu with a string that will show up in the status bar (if you have one) when you mouse over the menu item. i will leave that as an exercise for the reader to try though.

application Frame (networking) Space (architecture) Data Types Text editor A-Frame (virtual reality framework) Strings Pass (software)

Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Using AI and Machine Learning To Create Software
  • ChatGPT — The Google Killer? The Job Taker? Or Just a Fancy Chatbot?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: