wxpython GUI 如何从sql server实时获取数据显示出来?

weixin_44263578 2020-10-10 02:13:51
大家好,

我有一个简单的应用,想从sql server读取数据,并实时显示在wxpython编写的GUI 界面上,目前我可以做到从sql server读出数据来,但是做不到实时显示,也就是说当数据库的数据变化时,GUI界面上的数据不能同步更新,请高手帮忙指点一下,跪谢。
下面附上我的代码:Python 3.5 , Win10


# -*- 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()
...全文
3810 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
龚建波 2020-10-11
  • 打赏
  • 举报
回复
你发错分区了

16,817

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧