Preloader
Python
  • Estimated reading time: 1 Minute

How to center a window on the screen in WxPython

A tipical behaviour of most applications is to start on the center of the screen in order to use the app immediately. With the WxPython library this is pretty easy to achieve. Instead of retrieving the dimensions of the screen by yourself and calculating its position according to the size of your window etc. WxPython does this automatically for you if you call the Centre method from a Frame:

import wx

# Initialize a simple Window
app = wx.App()
frame = wx.Frame(None, title='Simple application')

# Center it with the 'Centre' method
frame.Centre()

# Show your main windows
frame.Show()
app.MainLoop()

Automatically the window will be centered on the screen independently of its own dimensions.

Example within an application context

If your application is structured, you may have your window extending a the Frame class of WxPython:

import wx

class MyApp(wx.Frame):

    def __init__(self, parent, title):
        super(MyApp, self).__init__(
            parent, 
            title = title,
            # Define the size of the window
            size=(800, 600)
        )

        ## Important: call the Centre method that centers automatically
        ## the window for you 
        self.Centre()


def main():

    app = wx.App()
    ex = MyApp(None, title='Example of Centering a Window')
    ex.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()

As result, you window will be centered as mentioned:

Center Window on Screen with WxPython

Happy coding !

Share:
Carlos Delgado

Carlos Delgado

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Related articles
How to Call SAP OData Services with Python
8 Feb, 2026
  • Estimated reading time: 6 Minutes
How to Learn Web Scraping with Python? Step-by-Step Tutorial
15 May, 2024
  • Estimated reading time: 5 Minutes
Tips And Tricks To Master PDF Processing With Python
27 Feb, 2023
  • Estimated reading time: 5 Minutes
How We Can Use Object-Oriented Programming with Python?
20 Jan, 2023
  • Estimated reading time: 5 Minutes
In what areas should you use Python?
7 Jan, 2023
  • Estimated reading time: 4 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.