如何使用用户输入更新TextControl值

时间:2019-04-14 02:15:50

标签: python-3.x wxpython-phoenix

我有一个带文本控件的Python表单,当用户单击“保存”时,需要将其保存到SQL表中。保存的唯一字段是我使用SetValue()(author_field和sate_field)并具有默认值的字段。如果用户更改了这些字段,如何将其保存到SQL?我需要一个变更事件吗?

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()```

1 个答案:

答案 0 :(得分:0)

我正在使用它。

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