In this Python GUI article i want to show you Creating Button in wxPython. also we are going to create Event Handler for our Button.
What is wxPython Button?
A button is a control that contains a text string, and it is one of the most common elements of a GUI. It may be placed on a dialog box or on a wx.Panel panel, or indeed on almost any other window.
Python GUI Creating Button in wxPython
Now let’s create our wxPython Button, This is the complete code for creating Button 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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title, size=(600, 400)) # Set window icon self.SetIcon(wx.Icon("codeloop.png", wx.BITMAP_TYPE_PNG)) # Create panel self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) # Horizontal box sizer sizer = wx.BoxSizer(wx.HORIZONTAL) # Static text label self.label = wx.StaticText(self, label="Changed Text") sizer.Add(self.label, 1, wx.EXPAND) # Button self.btn = wx.Button(self, label="Click Me") sizer.Add(self.btn, 0) # Bind button click event self.btn.Bind(wx.EVT_BUTTON, self.onClickMe) # Set sizer self.SetSizer(sizer) def onClickMe(self, event): self.label.SetLabelText("Welcome to codeloop.org") class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - wxPython Button") self.frame.Show() return True app = MyApp() app.MainLoop() |
This is our MyFrame class that inherits from wx.Frame. and it is our top level window. and also we create the object of MyPanel in here
1 2 3 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,400)) |
So this is MyPanel class that inherits from wx.Panel and in this class we create our Button in here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) sizer = wx.BoxSizer(wx.HORIZONTAL) self.label = wx.StaticText(self, label = "Changed Text") sizer.Add(self.label, 1, wx.EXPAND) self.btn = wx.Button(self, label = "Click Me") sizer.Add(self.btn, 0) self.btn.Bind(wx.EVT_BUTTON, self.onClickMe) self.SetSizer(sizer) |
These codes are for creating our Button and also Sizer for Button widget.
1 2 |
self.btn = wx.Button(self, label = "Click Me") sizer.Add(self.btn, 0) |
This is the method that we create our command in here and i want when a user click on the button i want to change the my label that i have in the label
1 2 |
def onClickMe(self, event): self.label.SetLabelText("Welcome to codeloop.org") |
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="Buttons") self.frame.Show() return True app = MyApp() app.MainLoop() |
So run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email