Welcome folks today in this blog post we will be looking at wxpython submit form data and display it in popup or modal window
. All the full source code of the application is given below.
Get Started
In order to get started you need to install the below library using the pip
command as shown below
pip install wxpython
After installing this library you need to make an app.py
file and copy paste the following code
app.py
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import wx import pprint about_txt = """\ The validator used in this example shows how the validator can be used to transfer data to and from each text control automatically when the dialog is shown and dismissed.""" class DataXferValidator(wx.PyValidator): def __init__(self, data, key): wx.PyValidator.__init__(self) self.data = data self.key = key def Clone(self): """ Note that every validator must implement the Clone() method. """ return DataXferValidator(self.data, self.key) def Validate(self, win): return True def TransferToWindow(self): textCtrl = self.GetWindow() textCtrl.SetValue(self.data.get(self.key, "")) return True def TransferFromWindow(self): textCtrl = self.GetWindow() self.data[self.key] = textCtrl.GetValue() return True class MyDialog(wx.Dialog): def __init__(self, data): wx.Dialog.__init__(self, None, -1, "Validators: data transfer") # Create the text controls about = wx.StaticText(self, -1, about_txt) name_l = wx.StaticText(self, -1, "Name:") email_l = wx.StaticText(self, -1, "Email:") phone_l = wx.StaticText(self, -1, "Phone:") name_t = wx.TextCtrl(self, validator=DataXferValidator(data, "name")) email_t = wx.TextCtrl(self, validator=DataXferValidator(data, "email")) phone_t = wx.TextCtrl(self, validator=DataXferValidator(data, "phone")) # Use standard button IDs okay = wx.Button(self, wx.ID_OK) okay.SetDefault() cancel = wx.Button(self, wx.ID_CANCEL) # Layout with sizers sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(about, 0, wx.ALL, 5) sizer.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5) fgs = wx.FlexGridSizer(3, 2, 5, 5) fgs.Add(name_l, 0, wx.ALIGN_RIGHT) fgs.Add(name_t, 0, wx.EXPAND) fgs.Add(email_l, 0, wx.ALIGN_RIGHT) fgs.Add(email_t, 0, wx.EXPAND) fgs.Add(phone_l, 0, wx.ALIGN_RIGHT) fgs.Add(phone_t, 0, wx.EXPAND) fgs.AddGrowableCol(1) sizer.Add(fgs, 0, wx.EXPAND|wx.ALL, 5) btns = wx.StdDialogButtonSizer() btns.AddButton(okay) btns.AddButton(cancel) btns.Realize() sizer.Add(btns, 0, wx.EXPAND|wx.ALL, 5) self.SetSizer(sizer) sizer.Fit(self) app = wx.PySimpleApp() data = { "name" : "Jordyn Dunn" } dlg = MyDialog(data) dlg.ShowModal() dlg.Destroy() wx.MessageBox("You entered these values:\n\n" + pprint.pformat(data)) app.MainLoop() |
If you execute the above python script
by typing the below command
python app.py