In this wxPython article i want to show you How To Create StatusBar in wxPython, first of all let’s talk about statusbar in wxPython.
What is wxPython StatusBar?
StatusBar in wxPython is a control, and it is used at the bottom of a frame to display status information to the user. It can display text messages and provide feedback about the application’s current state or progress of operations.
Features of wx.StatusBar
- Simple Status Messages: Display text messages in different fields.
- Multiple Fields: Split the status bar into multiple fields to show different pieces of information simultaneously.
- Size Grip: The status bar can include a size grip to allow the user to resize the window.
How to Create StatusBar in wxPython?
Now let’s create our example, this is the complete code for creating StatusBar in wxPython
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title, size=(600, 400)) # Set icon for the frame self.SetIcon(wx.Icon("codeloop.png", wx.BITMAP_TYPE_PNG)) # Create status bar with no border style self.statusBar = self.CreateStatusBar(style=wx.BORDER_NONE) # Set style of the status bar to flat self.statusBar.SetStatusStyles([wx.SB_FLAT]) # Set background color of the status bar to green self.statusBar.SetBackgroundColour('green') # Set initial text of the status bar self.statusBar.SetStatusText("Welcome to codeloop.org") # Initialize panel self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) # Additional panel components can be added here class MyApp(wx.App): def OnInit(self): # Initialize the main frame with a title and show it self.frame = MyFrame(parent=None, title="Codeloop.org - WxPython StatusBar") self.frame.Show() return True # Entry point of the application app = MyApp() app.MainLoop() |
At the top we have created our MyFrame. this class is a top level window that inherits from wx.Frame and we create the object of our MyPanel class in here. also we create our StatusBar in this class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,400)) self.statusBar = self.CreateStatusBar(style=wx.BORDER_NONE) self.statusBar.SetStatusStyles([wx.SB_FLAT]) self.statusBar.SetBackgroundColour('gray') self.statusBar.SetStatusText("This Is status Bar") self.panel = MyPanel(self) |
This is MyPanel class and in this article it is empty, because when you want to add StatusBar you should create that in the top level window.
1 2 3 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) |
So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects. and start our main loop.That’s it. Once the application’s main event loop processing takes over, control passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a human user clicking with a mouse and typing at the keyboard. When all the frames in an application have been closed, the app.MainLoop() method will return and the program will exit.
1 2 3 4 5 6 7 8 9 10 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - Wxpython Status Bar") self.frame.Show() return True app = MyApp() app.MainLoop() |
Run the code and this is the result
Subscribe and Get Free Video Courses & Articles in your Email