Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
wxPython Application Development Cookbook

You're reading from   wxPython Application Development Cookbook Over 80 step-by-step recipes to get you up to speed with building your own wxPython applications

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785287732
Length 264 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Cody Precord Cody Precord
Author Profile Icon Cody Precord
Cody Precord
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. wxPython Starting Points FREE CHAPTER 2. Common User Controls 3. UI Layout and Organization 4. Containers and Advanced Controls 5. Data Displays and Grids 6. Ways to Notify and Alert 7. Requesting and Retrieving Information 8. User Interface Primitives 9. Creating and Customizing Components 10. Getting Your Application Ready for Release Index

Adding the main frame

Most applications have some sort of main window that they want to show to allow their users to interact with the software. In wxPython, this window is called a frame. The frame is the main top-level container and the base for building most user interfaces in wxPython. This recipe will show how to create a frame and add it to an application.

How to do it…

You can do this by performing the following steps:

  1. Start by making a subclass of wx.Frame with the following code:
    class MyFrame(wx.Frame):
        def __init__(self, parent, title=""):
            super(MyFrame, self).__init__(parent, title=title)
            
            # Set an application icon
            self.SetIcon(wx.Icon("appIcon.png"))
            
            # Set the panel
            self.panel = wx.Panel(self)
  2. Next, create an instance of the frame and show it using the following code:
    class MyApp(wx.App):
        def OnInit(self):
            self.frame = MyFrame(None, title="Main Frame")
            self.frame.Show()
            return True
  3. Run the script and take a look at what we made:
    How to do it…

How it works…

The Frame class creates a top-level window that can be used to present any number of other controls. A frame can be created without any parent window and will remain in the application until it is dismissed by the user.

In this recipe, we set up a couple of items on the MyFrame class:

  1. We called SetIcon to set the custom application icon on the title bar of the frame. This icon was created from the appIcon.png file, which exists in the same directory as the script.
  2. We created a Panel object and set the frame as its parent object. A panel is a plain rectangular control used to contain other controls and is shown as the rectangular area inside the frame's borders. A panel must have a parent window in order to be created.

Finally, in the App object's OnInit method, we created an instance of the frame specifying the title that we wanted to show in the frame's title bar and then called its Show method to display it on the screen. This recipe can be used as the preparation to create any wxPython application.

There's more…

The wx.Frame constructor has several style flags that can be specified in its constructor to modify its behavior and appearance.

Style flag

Description

wx.DEFAULT_FRAME_STYLE

This flag is a bit mask of all the other flags described in the following sections

wx.MINIMIZE_BOX

This displays the minimize button on the title bar

wx.MAXIMIZE_BOX

This displays the maximize button on the title bar

wx.RESIZE_BORDER

This allows the frame to be resized by the user

wx.CAPTION

This displays a title caption on the frames title bar

wx.CLOSE_BOX

This displays the close button on the title bar

wx.SYSTEM_MENU

This displays a system menu (the menu that appears when clicking on the frame icon on Windows)

wx.CLIP_CHILDREN

This eliminates the flicker caused by background repainting (Windows only)

These style flags can be passed in any combination using a bitwise operator to turn off any of the features that you may not want to provide on all frames. Multiple flags can be combined using a bitwise or operation.

You have been reading a chapter from
wxPython Application Development Cookbook
Published in: Dec 2015
Publisher:
ISBN-13: 9781785287732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image