Is there any way to retrieve real time data from sql server on GUI interface with wxpython ?

Hi there ,

I have a simple application about GUI with wxpython , I’d like to display real time value from database. Currently what I can do is to display the value from database , but I can not figure out how to make it REAL TIME , I mean when the data changed in database then I hope the data on this interface will synchronize with database. Hope it’s clear. Anyone who can help on this ? Any suggestion will be appreciated.

Here i attached my code: Python 3.5 , Win 10

# -*- coding: utf-8 -*-
import wx
import wx.adv
import wx.grid
import sys
import pyodbc
import time

bgcolor = (220,220,220)
class Mywin(wx.Frame):
    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title = title, size = (600,320))
        self.InitUI()

    def InitUI(self):
        nb = wx.Notebook(self)
        nb.AddPage(MyPanel3(nb), "Table")
        self.Centre()
        self.Show(True)

def retrieve_data_fromdb():
    # SQL Server configuration
    server = 'localhost'
    db = '***'
    user = 'sa'
    pwd = '******'

    src_db = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};SERVER=' + server + ';DATABASE=' + db + ';UID=' + user + ';PWD=' + pwd)
    cur = src_db.cursor()

    select = 'select * from real_time_test'

    cur.execute(select)
    rows = cur.fetchone()
    wind_spd = rows[0]
    site_pwr = rows[1]
    acv_pr_setpnt = rows[2]
    park_pbl_cap = rows[3]
    tol_cur = rows[4]
    tol_non_prod = rows[5]
    data = []
    data.append(wind_spd)
    data.append(site_pwr)
    data.append(acv_pr_setpnt)
    data.append(park_pbl_cap)
    data.append(tol_cur)
    data.append(tol_non_prod)
    return data
    cur.commit()
    cur.close()
    src_db.close()

class MyPanel3(wx.Panel):
    def __init__(self, parent):
        super(MyPanel3, self).__init__(parent)
        self.SetBackgroundColour(bgcolor)
        self.Bind(wx.EVT_PAINT, self.OnPaint)

        title_NDC = wx.StaticText(self, -1, " Real time signals ", (30, 22))
        title_NDC.SetForegroundColour((0, 0, 255))
        wx.StaticText(self, -1, "1. Wind Speed", (35, 75))
        wx.StaticText(self, -1, "2. Site Power", (35, 95))
        wx.StaticText(self, -1, "Instant", (300, 45))

        wx.StaticText(self, -1, "m/s", (340, 75))
        wx.StaticText(self, -1, "kW", (340, 95))

        a = retrieve_data_fromdb()
        wind_spd_val = wx.StaticText(self, -1, a[0], (300, 75))
        wind_spd_val.SetForegroundColour((0, 0, 255))


    def OnPaint(self, event):
        pdc = wx.PaintDC(self)
        gc = wx.GCDC(pdc)
        gc.Clear()

        brush_rec = wx.Brush(bgcolor)
        gc.SetBrush(brush_rec)
        gc.SetPen(wx.Pen("black", 2))

        x1 = 20
        y1 = 30
        w1 = 500
        h1 = 180
        radius = 3
        gc.DrawRoundedRectangle(x1, y1, w1, h1, radius)


ex = wx.App()
Mywin(None,'My example')
ex.MainLoop()

There’s two ways:

1) constantly poll the database

2) listen for signals from the database

Which of 1) or 2) makes sense depends on what your database can do.

Karsten