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
These codes are for creating our Button and also Sizer for Button widget.
Python
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
Python
1
2
defonClickMe(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.