In this Python GUI i want to show you Creating GridSizer in wxPython. so first of all let’s talk about GridSizer in wxPython.
What is GridSizer in wxPython?
A grid sizer is a sizer which lays out its children in a two-dimensional table with all table fields having the same size, the width of each field is the width of the widest child, the height of each field is the height of the tallest child.
Python GUI Creating GridSizer in wxPython
Now let’s create our GridSizer example, so this is the complete code for grid size 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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title, size=(600, 400)) # 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 a grid sizer gridSizer = wx.GridSizer(4, 4, 5, 5) # Add buttons to the grid sizer for i in range(1, 17): btn = "Btn" + str(i) gridSizer.Add(wx.Button(self, label=btn), 0, wx.EXPAND) # Set the panel's sizer self.SetSizer(gridSizer) class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - GridSizer") self.frame.Show() return True # Create the application app = MyApp() # Run application event loop app.MainLoop() |
This is our MyFrame class that inherits from wx.Frame. and it is our top level window. and also we have created the object of MyPanel in here.
1 2 3 4 5 6 7 8 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size= (600,400)) self.panel = MyPanel(self) |
So this is MyPanel class that inherits from wx.Panel and in this class we create our GridSizer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) gridSizer = wx.GridSizer(4,4,5,5) for i in range(1,17): btn = "Btn" + str(i) gridSizer.Add(wx.Button(self, label = btn),0,wx.EXPAND ) self.SetSizer(gridSizer) |
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 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - GridSizer") self.frame.Show() return True |
Run the code and this will be the result
FAQs:
What is wxPython?
wxPython is a GUI toolkit for Python programming language.Using wxPython you can create cross-platform desktop applications with native look and feel using a single codebase. wxPython is built on top of the wxWidgets C++ library and provides different widgets, event handling mechanisms and other features for building graphical user interfaces.
How to create a window in wxPython?
For creating a window in wxPython, you need to subclass the wx.Frame class. This is a basic example:
1 2 3 4 5 6 7 8 9 10 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title, size=(400, 300)) self.Show(True) app = wx.App(False) frame = MyFrame(None, "Codeloop.org") app.MainLoop() |
Is wxPython free to use?
Yes, wxPython is an open-source library, and it is released under wxWindows Library Licence, And that is a modified version of the LGPL. This means that you can use wxPython for both open-source and commercial projects without any licensing fees.
Subscribe and Get Free Video Courses & Articles in your Email