Learn how to center a window on the screen with the GUI library 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

# Show your main windows

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__(
            title = title,
            # Define the size of the window
            size=(800, 600)

        ## Important: call the Centre method that centers automatically
        ## the window for you 

def main():

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

if __name__ == '__main__':

As result, you window will be centered as mentioned:

Center Window on Screen with WxPython

Happy coding !

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.