In this Python GUI article i want to show you Creating Panel in wxPython. first of all let’s talk about wxPython Panel.
What is wxPython Panel in Python GUI?
Basically in every wxpython application we have a frame that inherits from wx.Frame and that is top level window and in the frame we have panel that inherits from wx.Panel and in the panel we can add our widgets like buttons, labels and etc.
Python GUI Creating Panel in wxPython
Now let’s create our example on building wxPython Panel, So now let me write the complete code for this article.
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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): # Initialize frame with specified title and size super(MyFrame, self).__init__(parent, title=title, size=(800, 600)) # Set window icon icon = wx.Icon("codeloop.png", wx.BITMAP_TYPE_PNG) self.SetIcon(icon) # Create and set the panel self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): # Initialize the panel with the parent super(MyPanel, self).__init__(parent) # Panel contents go here class MyApp(wx.App): def OnInit(self): # Create and show the main frame self.frame = MyFrame(parent=None, title="Codeloo.org - Panel Window") self.frame.Show() return True # Create the application instance app = MyApp() # Run the application event loop app.MainLoop() |
So as i have mentioned in my previous article on wxPython GUI Development , this class is our main MyFrame class that inherits from wx.Frame , and it is the container for our widgets and panel. you can see that we have created the object of MyPanel class in this class.
1 2 3 4 5 6 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (800,600)) self.panel = MyPanel(self) |
OK now this class is MyPanel class whatever name you want you can give for our class. this class inherits from wx.Panel. for right now this is an empty panel and we dont have any widgets in this panel, but i have told that this is the place that we can create our widgets.
1 2 3 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) |
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 pri marily 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="Panel Window") self.frame.Show() return True app = MyApp() app.MainLoop() |
Run the complete code this will be the result
FAQs:
Is there a GUI for Python?
Yes, there are several GUI (Graphical User Interface) libraries available for Python, using these Python GUI libraries you can create graphical desktop applications. Some popular GUI libraries for Python are Tkinter, PyQt6, wxPython, Kivy, PySide6 and PyGTK.
What is the fastest Python GUI?
It’s challenging that we talk about the fastest Python GUI, because the performance of a GUI Framework depends on different factors such as complexity of the application, efficiency of the GUI library’s implementation, and the underlying platform’s capabilities. but in general we can say that some GUI libraries like PyQt and wxPython are fast, because they have C/C++ implementations in thier underlying. Also Kivy is good for its high-performance graphics rendering, and that will be the best choice for applications with demanding graphical requirements.
Subscribe and Get Free Video Courses & Articles in your Email