Search code examples
python-3.xwxpython

How do I update TextControl value with user input


I have a Python form with textcontrols, which need to save to a SQL table when the user hits Save. The only fields saving are the ones where I use SetValue() (author_field and sate_field) and have a default value. If the user changes these fields how can I save this to SQL? Do I need an on change event?

author_field=wx.TextCtrl(panel, pos=(20, 40))
species_field=wx.ComboBox(panel, pos=(150, 40), choices = row)
location_field=wx.TextCtrl(panel, pos=(20, 100))
date_field = wx.adv.DatePickerCtrl(panel, pos=(150, 100))

author_field.SetValue(getpass.getuser()) #AD user
date_field.SetValue(now)

auth=author_field.GetValue()
datefield=date_field.GetValue()
spec=species_field.GetValue()
locatval=location_field.GetValue()```

Solution

  • I have it working.

    self.author_field=wx.TextCtrl(panel, wx.ID_ANY, getpass.getuser(), pos=(20, 40))
        self.species_field=wx.ComboBox(panel, wx.ID_ANY, pos=(150, 40), choices = row)
        self.location_field=wx.TextCtrl(panel, wx.ID_ANY, pos=(20, 100))
        self.date_field = wx.adv.DatePickerCtrl(panel, wx.ID_ANY, now, pos=(150, 100))      
        SaveData = wx.Button(panel, label='Save', pos=(20, 140))
        SaveData.Bind(wx.EVT_BUTTON, self.SaveSQL)
    
    def SaveSQL(self, e):
    
        if self.location_field.Value is not None:
            conn = pymssql.connect("server name", "database", "username", "password")
            cursor = conn.cursor()
            sql_insert_query = """INSERT INTO Table ([User], DateEntered, SpeciesID, [Location]) VALUES (%s, %s, %s, %s)"""
    
            insert_tuple = (self.author_field.Value, self._wxdate2pydate(self.date_field.Value), self.species_field.Value, self.location_field.Value)       
    
            result = cursor.execute(sql_insert_query, insert_tuple)
    
            conn.commit()
            cursor.close()
    
            wx.MessageBox('Form Saved', 'Info', wx.OK | wx.ICON_INFORMATION)
        elif self.location_field.Value is None:
    
            wx.MessageBox('Please select Species', 'Info', wx.OK | wx.ICON_INFORMATION)
            return