13.07.2015 Views

The wxPython tutorial

The wxPython tutorial

The wxPython tutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Working with databaseshttp://www.zetcode.com/wxpython/databases/Putting it togetherSo far we have been looking at the SQLite3 library, databases andSQL language. Now it is time to put it all together with <strong>wxPython</strong> in asimple functional script. <strong>The</strong> next simple script will do only onespecific thing. Insert data into a table. We will use our peopledatabase, neigbours table.#!/usr/bin/python# insertdata.pyimport wximport sqlite3 as liteclass InsertData(wx.Frame):def __init__(self, parent, id, title):wx.Frame.__init__(self, parent, id, title, size=(280, 200))panel = wx.Panel(self, -1)gs = wx.FlexGridSizer(3, 2, 9, 9)vbox = wx.BoxSizer(wx.VERTICAL)hbox = wx.BoxSizer(wx.HORIZONTAL)name = wx.StaticText(panel, -1, 'Name')remark = wx.StaticText(panel, -1, 'Remark')age = wx.StaticText(panel, -1, 'Age')self.sp = wx.SpinCtrl(panel, -1, '', size=(60, -1), min=1, max=125)self.tc1 = wx.TextCtrl(panel, -1, size=(150, -1))self.tc2 = wx.TextCtrl(panel, -1, size=(150, -1))gs.AddMany([(name), (self.tc1, 1, wx.LEFT, 10),(remark), (self.tc2, 1, wx.LEFT, 10),(age), (self.sp, 0, wx.LEFT, 10)])vbox.Add(gs, 0, wx.ALL, 10)vbox.Add((-1, 30))insert = wx.Button(panel, -1, 'Insert', size=(-1, 30))cancel = wx.Button(panel, -1, 'Cancel', size=(-1, 30))hbox.Add(insert)hbox.Add(cancel, 0, wx.LEFT, 5)vbox.Add(hbox, 0, wx.ALIGN_CENTER | wx.BOTTOM, 10)self.Bind(wx.EVT_BUTTON, self.OnInsert, id=insert.GetId())self.Bind(wx.EVT_BUTTON, self.OnCancel, id=cancel.GetId())panel.SetSizer(vbox)self.Centre()self.Show(True)def OnInsert(self, event):try:con = lite.connect('people')cur = con.cursor()10 de 12 27/04/2008 1:06

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!