In this wxPython tutorial let us create a button widget from the previously created DerivedApp class. After the child class of the ‘wx.App’ main class has been created, things are getting really simple because I can then create those widgets within the methods of the child class.
The wx.Button widget basically has eight parameters but you do not need to include all of them, those eight parameters are as follows:-
parent (wx.Window) – Parent window. Must not be None. id (wx.WindowID) – Button identifier. A value of ID_ANY indicates a default value. label (string) – Text to be displayed on the button. pos (wx.Point) – Button position. size (wx.Size) – Button size. If the default size is specified then the button is sized appropriately for the text. style (long) – Window style. See wx.Button class description. validator (wx.Validator) – Window validator. name (string) – Window name.
I have only included a few of them as follows:-
self.the_button = wx.Button(parent = self.the_frame, id=1, label=”Hello World!”, pos=wx.Point(150, 150), name=”Hello!”)
Except for the parent value, the value of the others is optional. And as you might have guessed, the wx.Point class will place the button based on the x and y coordinate on the top-level window object.
In the below program, the wxapp.CreateWidgets() method will call the wxapp.CreateButton() method and other methods (in the future) to create all the nessasery widgets for the top-level window.
import wx class DerivedApp(wx.App): def OnInit(self): self.the_frame = wx.Frame(None, -1) # create the top frame self.the_frame.Show(True) return True def CreateWidgets(self): # create various widgets self.CreateButton() def CreateButton(self): self.the_button = wx.Button(self.the_frame, id=1, label="Hello World!", pos=wx.Point(150, 150), name="Hello!") wxapp = DerivedApp() wxapp.CreateWidgets() # initialise all widgets wxapp.MainLoop()
As you can see from above program it is better for us to create one main child class from the wx.App class which will then allow us to create our own methods within it later on!
Below is the outcome of the above program.
There are other wxPython button widgets and I will go through them one by one in the next few chapters!