In this wxPython article we want to learn about How to Create wxPython Main Window wx.Frame, so wxPython is popular library for creating desktop applications with Python. one of the main components of wxPython application is main window or the frame. in this article we want to talk about creating wxPython main window (wx.Frame) using the wxPython library.
How to Create wxPython Main Window wx.Frame
First of all you need to install wxPython and you can use pip for that
1 |
pip install wxpython |
First step in creating wxPython main window is to import the wxPython library. you can do this by adding the following line ode to your Python code.
1 |
import wx |
After that you have imported wxPython library, next step is to create new wx.Frame object. you can do this by calling wx.Frame() constructor and passing in the parent window object, in here we have added title of the window and added size of the window.
1 2 3 |
class MyFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, title=title, size=(800, 600)) |
In the above example we have created a class called MyFrame that extends from wx.Frame. we have also defined a constructor method for the class that takes two arguments, parent and title. parent argument is used to specify the parent window object, while title argument is used to specify the title of the window. we have also specified the initial size of the window to be 800×600 pixels.
After that you have created new wx.Frame object, next step is to show the window. you can do this by calling the Show() method on the wx.Frame object.
1 2 3 4 |
app = wx.App() frame = MyFrame(None, "wxPython - geekscoders.com") frame.Show() app.MainLoop() |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, title=title, size=(800, 600)) app = wx.App() frame = MyFrame(None, "wxPython - geekscoders.com") frame.Show() app.MainLoop() |
Run the complete code and this will be the result