有什么方法可以使用wxpython在GUI界面上从sql server检索实时数据吗?

问题描述

我有一个有关wxpython的GUI的简单应用程序,我想显示数据库中的实时值。当前,我能做的是显示数据库中的值,但是我无法弄清楚如何使它成为REAL TIME,我的意思是当数据库中的数据更改时,我希望此接口上的数据将与数据库同步。希望清楚。有人可以帮忙吗?任何建议将不胜感激。

在这里附加了我的代码: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,255))
        wx.StaticText(self,"1. Wind Speed",(35,75))
        wx.StaticText(self,"2. Site Power",95))
        wx.StaticText(self,"Instant",(300,45))

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

        a = retrieve_data_fromdb()
        wind_spd_val = wx.StaticText(self,a[0],75))
        wind_spd_val.SetForegroundColour((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()

解决方法

使用简单的{ "compilerOptions": { "target": "es5","lib": [ "dom","dom.iterable","esnext" ],"allowJs": true,"skipLibCheck": true,"esModuleInterop": true,"allowSyntheticDefaultImports": true,"strict": true,"forceConsistentCasingInFileNames": true,"module": "esnext","moduleResolution": "node","resolveJsonModule": true,"isolatedModules": true,"noEmit": true,"jsx": "react" },"include": [ "src" ] 是实现这一目标的一种简单(即使效率低下)方法。
只需每x时间读取一次数据库并更新屏幕。

原谅凌乱的代码,但是您会明白的。
使用输入框更新值。
您将不得不弄弄数据库代码,因为我已经使用了sqlite3,它已经在我的盒子里了。

Timer

enter image description here