In this wxPython example tutorial, I will write the program to call a function which will then create a dialog box after a button has been clicked.
First of all, from the previous program, I have to add the below line under the CreateButton function which will call another function after the button has been clicked.
self.the_button.Bind(wx.EVT_BUTTON, self.OnButton)
The reason I have created the function separately instead of putting all the commands under one single function is that I want to reuse the OnButton function again in future code.
def OnButton(self, event): self.CreateDialogBox()
As you can see once the button has been clicked the OnButton function will get called based on the button’s Bind method.
def CreateDialogBox(self): self.dialogbox = wx.Dialog(parent=self.the_frame, id=5, title="Hello Enter", name="Hello") self.dialogbox.Show()
At last, the above method has been called and the dialog box appears after the show method has been called!
According to the wxPython official site, the dialog widget has the below parameters but I am not going to use all of them.
Parameters parent (wx.Window) – Can be None, a frame or another dialog box. id (wx.WindowID) – An identifier for the dialog. A value of -1 is taken to mean a default. title (string) – The title of the dialog. pos (wx.Point) – The dialog position. The value DefaultPosition indicates a default position, chosen by either the windowing system or wxWidgets, depending on platform. size (wx.Size) – The dialog size. The value DefaultSize indicates a default size, chosen by either the windowing system or wxWidgets, depending on platform. style (long) – The window style. name (string) – Used to associate a name with the window, allowing the application user to set Motif resource values for individual dialog boxes.
The outcome is as follows:-
There are other things you can do with this dialog box thus please don’t hesitate to try it out!