wxPython: Making your Frame Maximize or Full Screen

This topic comes up from time to time every year. Someone will ask about how to make their application full screen or how to just make it maximize when it first loads. So we’ll spend a little time showing you how to do each of these activities.

How to Maximize Your Frame

Occasionally you’ll want your wxPython application to be maximized when you first load it. Or perhaps you’ll want to have a sub-frame maximized. This is really easy to do in wxPython, but there is a little “gotcha” to watch out for. Let’s look at the code before we discuss the gotcha though:

import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent)
    
########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """"""
        wx.Frame.__init__(self, None, title="Test Maximize")
        panel = MyPanel(self)
        self.Show()
        self.Maximize(True)
        
        
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()
    

Here we have a pretty standard set up using two classes, one a sub-class of wx.Panel and the other a sub-class of wx.Frame. To get it to Maximize, we just call the frame’s Maximize() method. Here’s where the gotcha comes in though. If you call Maximize before you call Show, you may see a glitch. For example, when I called Maximize first on Windows 7, the panel didn’t stretch to fully cover the frame correctly. See screenshot below:

wxMaxGlitch

As you can see, the frame is showing a little on the right hand side and along the bottom (the darker grey). So if you run the code above, the panel will cover the frame like it’s supposed to and it will look uniform. Occasionally you might need to also call your frame’s Raise() method to make it show up on top or at least cause the taskbar to blink a bit to get the user’s attention.

Making wxPython Show FullScreen

Personally, I haven’t found any good use cases for going full screen (i.e. covering up the entire screen) except for maybe a screen save type application or perhaps a photo viewer. But regardless, here’s the usual way of accomplishing this task:

import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        
        self.Bind(wx.EVT_KEY_DOWN, self.onKey)
        
    #----------------------------------------------------------------------
    def onKey(self, event):
        """
        Check for ESC key press and exit is ESC is pressed
        """
        key_code = event.GetKeyCode()
        if key_code == wx.WXK_ESCAPE:
            self.GetParent().Close()
        else:
            event.Skip()
        
    
########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test FullScreen")
        panel = MyPanel(self)
        self.ShowFullScreen(True)
        
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

Note that because the application is full screen, there is to title bar with a close button, so there’s no good way to close the application. Thus I added an event handler for key events such that the user can press ESC to close the application.

Wrapping Up

Now you should be familiar with how to make your own wxPython application go into a maximized state or even full screen. Good luck and happy coding!

Additional Resources

3 thoughts on “wxPython: Making your Frame Maximize or Full Screen”

Comments are closed.