In this Python GUI Development i want to show Creating Layouts in wxPython. so first of all let’s talk about Layouts in wxPython.
How to Use Layouts in wxPython?
In wxPython you can use wx.BoxSizer for creating Layouts. the basic idea behind a box sizer is that windows will most be laid out in rather simple basic geometry. typically in a row or a column or several hierarchies of either. and there are different box sizers in wxPython.
Python GUI Creating Layouts in wxPython
Now let’s create our example, so this is the complete code for Python GUI Creating Layouts in wxPython
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title) # Set window icon icon = wx.Icon("codeloop.png", wx.BITMAP_TYPE_PNG) self.SetIcon(icon) self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) # Create vertical and horizontal box sizers vboxsizer = wx.BoxSizer(wx.VERTICAL) hboxsizer = wx.BoxSizer(wx.HORIZONTAL) # Add labels to the vertical sizer self.label = wx.StaticText(self, label="This is a label", style=wx.ALIGN_CENTER) vboxsizer.Add(self.label, 0, wx.EXPAND) self.label2 = wx.StaticText(self, label="This is Second label", style=wx.ALIGN_CENTER) vboxsizer.Add(self.label2, 0, wx.EXPAND) # Add labels to the horizontal sizer self.label3 = wx.StaticText(self, label="This is third label", style=wx.ALIGN_CENTER) hboxsizer.Add(self.label3, 0, wx.EXPAND) self.label4 = wx.StaticText(self, label="This is fourth label", style=wx.ALIGN_CENTER) hboxsizer.Add(self.label4, 0, wx.EXPAND, wx.ALIGN_RIGHT, 20) hboxsizer.AddStretchSpacer(1) # Add the horizontal sizer to the vertical sizer vboxsizer.Add(hboxsizer, 1, wx.ALL | wx.EXPAND) # Set the panel's sizer self.SetSizer(vboxsizer) class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - wxPython Sizers") self.frame.Show() return True # Create the application app = MyApp() # Run the application event loop app.MainLoop() |
We have three classes. the first class is our MyFrame class that inherits from wx.Frame. and it is the container for our widgets and also we have created the object of MyPanel class in this class.
1 2 3 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title) |
So this is the second class and important class for creating our widgets like label, button, menu and etc. MyPanel class inherits from wx.Panel. and we create the constructor for the class. in this class we create our two box sizers wx.VERTICAL and wx.HORIZONTAL. also we create labels because we are going to add these labels in our box sizer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) vboxsizer = wx.BoxSizer(wx.VERTICAL) hboxsizer = wx.BoxSizer(wx.HORIZONTAL) self.label = wx.StaticText(self, label = "This is a label", style = wx.ALIGN_CENTER) vboxsizer.Add(self.label,0, wx.EXPAND) self.label2 = wx.StaticText(self, label="This is Second label", style=wx.ALIGN_CENTER) vboxsizer.Add(self.label2, 0, wx.EXPAND) self.label3 = wx.StaticText(self, label="This is third label", style=wx.ALIGN_CENTER) hboxsizer.Add(self.label3, 0, wx.EXPAND) self.label4 = wx.StaticText(self, label="This is fourth label", style=wx.ALIGN_CENTER) hboxsizer.Add(self.label4, 0, wx.EXPAND, wx.ALIGN_RIGHT, 20) hboxsizer.AddStretchSpacer(1) vboxsizer.Add(hboxsizer, 1, wx.ALL | wx.EXPAND) self.SetSizer(vboxsizer) #self.SetSizer(hboxsizer) |
So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects. and start our main loop.That’s it. Once the application’s main event loop processing takes over, control passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a human user clicking with a mouse and typing at the keyboard. When all the frames in an application have been closed, the app.MainLoop() method will return and the program will exit.
1 2 3 4 5 6 7 8 9 10 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - wxPython Sizers") self.frame.Show() return True app = MyApp() app.MainLoop() |
Run the code and this is the result
FAQs:
How to Install wxPython?
wxPython installation is easy and simple, you can just use pip for wxpython installation, open your terminal or command prompt and write this command.
1 |
pip install wxPython |
What is wxPython used for?
wxPython is a GUI library for Python programming language, wxPython is used for building create cross-platform desktop applications. wxPython provides Python bindings for wxWidgets C++ library, using this Python GUI library you can build desktop applications for different platforms like Windows, macOS, and Linux
Is wxPython is better GUI than Tkinter?
So if we compare these two Python GUI frameworks, wxPython offers more advanced widgets, native look and feel across platforms, and better support for complex user interfaces. It also provides more object oriented approach to GUI development, and it is one of the best choice for building complex applications.
on the other hand Tkinter is included with Python’s standard library, it is easy accessible, and also it is easy to start with that, TKinter is the best choice for simple applications. because it is lightweight, easy to learn and suitable for quick prototyping and small projects.
Which is better, PyQt or wxPython?
So if we compare these two Python GUI libraries, PyQt is based on Qt framework, PyQt has different widgets, layouts and features for GUI development. also it offers different customization options, good documentation and a strong community.
on the other hand wxPython is based on the wxWidgets library and provides Python bindings for native-looking user interfaces on different platforms. It offers a more Pythonic API and better integration with the Python ecosystem.
Subscribe and Get Free Video Courses & Articles in your Email