In this Python GUI article i want to show you Creating Label in wxPython. so in this article our code will have three classes. and as i have mentioned in my previous topic that for creating widgets we need to create a MyPanel class that inherits from wx.Panel and in that panel we are going to create our label.
What is wxPython Label?
In wxPython label is a user interface element, and it is used for displaying text or images in window or panel. Labels are typically used to provide descriptive text or headings for other controls, such as buttons, text boxes or images.
In wxPython, you can create label using wx.StaticText class. You can create a label by instantiating an object of this class and providing the text content you want to display. You can also customize the appearance of the label, such as font size, font style, text alignment, and text color.
Python GUI Creating Label in wxPython
Now let’s create our example, this is the complete code for Python GUI Creating Label 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 |
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 label and position it on the panel self.label = wx.StaticText(self, label="This is a label", pos=(100, 100)) class MyApp(wx.App): def OnInit(self): # Create and show the main frame self.frame = MyFrame(parent=None, title="Codeloop.org - Adding Label") 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 4 5 6 7 8 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title) icon = wx.Icon("codeloop.png", wx.BITMAP_TYPE_PNG) self.SetIcon(icon) self.panel = MyPanel(self) |
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. after that it is time to create our label in wxPython. so in wxPython for creating Label we have wx.StaticText(). a static text control displays one or more lines of read-only text.
1 2 3 4 5 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) self.label = wx.StaticText(self, label = "This is a label", pos = (100,100)) |
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="Adding Label") self.frame.Show() return True app = MyApp() app.MainLoop() |
Run the complete code this will be the result
FAQs:
Is wxPython still in development?
Yes, wxPython is still actively developed and maintained by wxPython development team and community. New features, bug fixes and updates are regularly released to improve the functionality, stability and compatibility of wxPython across different platforms and Python versions.
Is wxPython easy to use?
wxPython is designed to be easy to use, especially for Python developers that they are familiar with other GUI libraries. wxPython provides Pythonic API that allows you to create cross-platform desktop applications with native look and feel using a familiar syntax and programming style.
Can I use PyQt for free?
Yes and no. PyQt is available under two different licenses, GNU General Public License (GPL) and a commercial license. If you are developing an open-source project and are willing to release your source code under the terms of the GPL, you can use PyQt for free. but if you are developing commercial application and do not want to release your source code, you will need to purchase a commercial license from Riverbank Computing.
Subscribe and Get Free Video Courses & Articles in your Email