In this wxPython article we want to learn about Creating User Interfaces with wxPython, User interface is important aspect of software development and creating nice and user friendly interfaces can make or break an application. wxPython is popular library for creating user interfaces in Python. in this article we want to talk about the basics of wxPython and how to create user interfaces using this powerful tool.
What is wxPython ?
wxPython is Python wrapper for the wxWidgets C++ library. it provides a set of classes and functions for creating graphical user interfaces (GUIs) that are platform independent and native looking. wxPython supports different widgets such as buttons, text boxes, labels, menus and many more, and it allows developers to create complex and sophisticated GUIs with easy.
Creating User Interfaces with wxPython
First of all we need to install wxPython
1 |
pip install wxPython |
After installation now let’s create our first simple GUI window in wxPython
1 2 3 4 5 6 |
import wx app = wx.App() frame = wx.Frame(None, title="Geekscoders.com") frame.Show(True) app.MainLoop() |
In the code above we have imported wx module and creates an instance of the wx.App class, which is required for every wxPython application. after that we have created wx.Frame object with the title Geekscoders.com and display it using the Show() method. and finally we have called the MainLoop() method of the wx.App instance to start the event loop, which handles user input and updates the GUI.
Run the code and this will be the result
Now that we have created our first window, let’s add some widgets to it. wxPython provides different widgets that you can use to create your user interface.
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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title) self.MyUI() def MyUI(self): panel = wx.Panel(self) button = wx.Button(panel, label="Click me!") button.Bind(wx.EVT_BUTTON, self.OnClick) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(button, 0, wx.ALIGN_CENTER | wx.TOP, 50) panel.SetSizer(sizer) def OnClick(self, event): print("Button clicked") if __name__ == "__main__": app = wx.App() frame = MyFrame(None, title="GeeksCoders.com") frame.Show(True) app.MainLoop() |
In the code above we have created new class called MyFrame that inherits from wx.Frame. in the MyUI() method, we have created wx.Panel object and add wx.Button widget to it. we also bind the button to the OnClick() method, which will be called when the user clicks the button. and lastly we set the panel’s sizer to a vertical box sizer and add the button to it.
Run the code and this will be the result
wxPython provides different events that you can handle in your user interface. events are triggered by user input such as clicking button, typing in text box or selecting an item from list.
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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title) self.MyUI() def MyUI(self): panel = wx.Panel(self) self.label = wx.StaticText(panel, label="Click the button!") button = wx.Button(panel, label="Click me!") button.Bind(wx.EVT_BUTTON, self.OnClick) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.label, 0, wx.ALIGN_CENTER | wx.TOP, 50) sizer.Add(button, 0, wx.ALIGN_CENTER | wx.TOP, 20) panel.SetSizer(sizer) def OnClick(self, event): self.label.SetLabel("Welcome to geekscoders.com") if __name__ == "__main__": app = wx.App() frame = MyFrame(None, title="Geekscoders.com") frame.Show(True) app.MainLoop() |
In the code above we have created new class called MyFrame that inherits from wx.Frame. in the MyUI() method we have created wx.Panel object and add wx.Button widget to it. we also bind the button to the OnClick() method which will be called when the user clicks the button. and finally we set the panel sizer to a vertical box sizer and add the button to it.
Handling events is an essential part of building user interfaces and wxPython provides simple way to do this. when a user interacts with a widget, wxPython generates an event that can be captured and handled by the application. for example when the user clicks a button, wxPython generates wx.EVT_BUTTON event which can be bound to a function that handles the event. in the OnClick() method we simply print a message to the console when the button is clicked.
This will be the result