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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • SRE vs. DevOps
  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • Top 10 Engineering KPIs Technical Leaders Should Know

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • SRE vs. DevOps
  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • Top 10 Engineering KPIs Technical Leaders Should Know
  1. DZone
  2. Data Engineering
  3. Data
  4. An Intro to StaticBox and StaticBoxSizers

An Intro to StaticBox and StaticBoxSizers

In this quick tutorial, a Python expert walks us through creating simple Graphical User Interfaces using the wxPython GUI toolkit.

Mike Driscoll user avatar by
Mike Driscoll
·
Updated May. 13, 19 · Tutorial
Like (1)
Save
Tweet
Share
4.84K Views

Join the DZone community and get the full member experience.

Join For Free

There are many widgets that are included with the wxPython GUI toolkit. One of them is a fairly handy widget called wx.StaticBox. This widget accepts a string and then will draw a box with the string in the upper left-hand corner of the box. However, this only works when you use it in conjunction with wx.StaticBoxSizer.

Here is an example of what one might look like:

Graphical User Interface

Simple Python-Based GUI


Now let's go ahead and write the code you would use to create the example above:

import wx
 
class MyPanel(wx.Panel):
 
    def __init__(self, parent):
        super().__init__(parent)
        box = wx.StaticBox(self, -1, "This is a wx.StaticBox")
        bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
 
        t = wx.StaticText(self, -1, "Controls placed \"inside\" the box are really its siblings")
        bsizer.Add(t, 0, wx.TOP|wx.LEFT, 10)
 
        border = wx.BoxSizer()
        border.Add(bsizer, 1, wx.EXPAND|wx.ALL, 25)
        self.SetSizer(border)
 
 
class MyFrame(wx.Frame):
 
    def __init__(self):
        super().__init__(None, title='Test')
        panel = MyPanel(self)
        self.Show()
 
if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

This code is based on the wx.StaticBox demo code from wxPython's demo application. Basically, you create the wx.StaticBox, add it to an instance of wx.StaticBoxSizer, and then add that to a wx.BoxSizer and you're done.

But what if you wanted a more complicated layout within the box?

Let's take a look at that use-case next!

Nesting Sizers in wx.StaticBoxSizer

More often then not, you will want more than a single widget inside of your box widget. When that happens, you will need to use sizers inside of your wx.StaticBoxSizer.

Here is an example:

Simple Python-Based GUI

Let's go ahead and take a look at the code for this example:

import wx
 
class MyPanel(wx.Panel):
 
    def __init__(self, parent):
        super().__init__(parent)
 
        box = wx.StaticBox(self, -1, "Group Name")
        bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
 
        hsizer = wx.BoxSizer()
 
        vsizer = wx.BoxSizer(wx.VERTICAL)
        lbl = wx.StaticText(self, label='label 1')
        vsizer.Add(lbl)
        cbo = wx.ComboBox(self, value='Python', choices=['Python', 'Ruby'],
                          size=(75, -1))
        vsizer.Add(cbo)
        hsizer.Add(vsizer)
 
        hsizer.AddSpacer(80)
 
        vsizer = wx.BoxSizer(wx.VERTICAL)
        lbl = wx.StaticText(self, label='label 2')
        vsizer.Add(lbl)
        cbo = wx.ComboBox(self, value='Ford', choices=['Ford', 'Chevrolet'],
                          size=(75, -1))
        vsizer.Add(cbo)
        hsizer.Add(vsizer)
 
        bsizer.Add(hsizer, 0, wx.CENTER)
 
 
        main_sizer = wx.BoxSizer()
        main_sizer.Add(bsizer, 1, wx.EXPAND | wx.ALL, 10)
        self.SetSizer(main_sizer)
 
class MyFrame(wx.Frame):
 
    def __init__(self):
        super().__init__(None, title='Test')
        panel = MyPanel(self)
        self.Show()
 
if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

In this case, you need to create two vertically oriented wx.BoxSizers to hold the four widgets in two columns. You add those sizers to a horizontally oriented wx.BoxSizer as well. If you wanted to simplify this a bit, you could use a wx.GridSizer instead of these BoxSizers.

Regardless of the approach, you end up with a nicely laid out application.

Wrapping Up

Using the wx.StaticBox widget is pretty straight-forward overall. I think it could be simpler if the widget and the sizer were combined into one class though. Anyway, if you'd like to learn more about this widget, you should see the documentation. Have fun and happy coding!

Use case application Strings Data Types Documentation Column (database) Coding (social sciences) Nesting (computing)

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

Opinions expressed by DZone contributors are their own.

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • SRE vs. DevOps
  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • Top 10 Engineering KPIs Technical Leaders Should Know

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

Let's be friends: