用Python编程好多年了,但是最近项目有个需求,就是用wxpython做一个小工具来实时显示一些数据。以前从来没有接触过wxpython,现在要用了,只能边学变卖,其他都还好,但就是实时显示这个功能一直实现不了,网上关于这方面的资料比较少,甚至Google都没有找到想要的答案。经过半个月的琢磨,终于搞明白了,现在跟大家分享一些,让大家少踩坑。
Python 3.5 + Win10 实测成功
# -*- coding: utf-8 -*-import wximport wx.advimport wx.gridimport sysimport pyodbcimport timebgcolor = (220,220,220)class Mywin(wx.Frame): def __init__(self, parent, title): super(Mywin, self).__init__(parent, title = title, size = (1180,620)) self.InitUI() def InitUI(self): nb = wx.Notebook(self) nb.AddPage(MyPanel3(nb), "Table") self.Centre() self.Show(True)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, " NDC 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, "3. Active power reference setpoint", (35, 115)) wx.StaticText(self, -1, "4. Park possible capability", (35, 135)) wx.StaticText(self, -1, "5. Total Curtailment", (35, 155)) wx.StaticText(self, -1, "6. Total Non Production", (35, 175)) wx.StaticText(self, -1, "Instant", (300, 45)) wx.StaticText(self, -1, "Accumulated", (400, 45)) wx.StaticText(self, -1, "m/s", (340, 75)) wx.StaticText(self, -1, "kW", (340, 95)) wx.StaticText(self, -1, "kW", (340, 115)) wx.StaticText(self, -1, "kW", (340, 135)) wx.StaticText(self, -1, "kW", (340, 155)) wx.StaticText(self, -1, "kW", (340, 175)) wx.StaticText(self, -1, "MWh", (470, 95)) wx.StaticText(self, -1, "MWh", (470, 135)) wx.StaticText(self, -1, "MWh", (470, 155)) wx.StaticText(self, -1, "MWh", (470, 175)) # Set the value accordingly a = self.retrieve_data_fromdb() self.wind_spd_val = wx.StaticText(self, -1, a[0], (300, 75)) self.site_pwr_val = wx.StaticText(self, -1, a[1], (300, 95)) self.acv_pr_setpnt_val = wx.StaticText(self, -1, a[2], (300, 115)) self.park_pbl_cap_val = wx.StaticText(self, -1, a[3], (300, 135)) self.tol_cur_val = wx.StaticText(self, -1, a[4], (300, 155)) self.tol_non_prod_val = wx.StaticText(self, -1, a[5], (300, 175)) # Set the color for each value self.wind_spd_val.SetForegroundColour((0, 0, 255)) self.site_pwr_val.SetForegroundColour((0, 0, 255)) self.acv_pr_setpnt_val.SetForegroundColour((0, 0, 255)) self.park_pbl_cap_val.SetForegroundColour((0, 0, 255)) self.tol_cur_val.SetForegroundColour((0, 0, 255)) self.tol_non_prod_val.SetForegroundColour((0, 0, 255)) # Bind the callbacks for the timer and the update function self.dbtimer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.dbtimer) self.dbtimer.Start(1000) title_WTG = wx.StaticText(self, -1, " WTG Summary ", (30, 222)) title_WTG.SetForegroundColour((0, 0, 255)) wx.StaticText(self, -1, "Count", (300, 240)) wx.StaticText(self, -1, "Sum of capability", (400, 240)) wx.StaticText(self, -1, "WTG w. Communication OK", (65, 270)) wx.StaticText(self, -1, "WTG Running", (65, 290)) wx.StaticText(self, -1, "WTG Paused by Curtailment", (65, 310)) wx.StaticText(self, -1, "WTG Not Available", (65, 330)) wx.StaticText(self, -1, "kW", (470, 290)) wx.StaticText(self, -1, "kW", (470, 310)) wx.StaticText(self, -1, "kW", (470, 330)) title_Measure = wx.StaticText(self, -1, " Measure location ", (570, 22)) title_Measure.SetForegroundColour((0, 0, 255)) wx.StaticText(self, -1, "Site Power Loss", (580, 160)) wx.StaticText(self, -1, "kW", (640, 180)) wx.StaticText(self, -1, "Measured Output", (920, 40)) wx.StaticText(self, -1, "Reference Setpoint", (920, 60)) wx.StaticText(self, -1, "kW", (1105, 40)) wx.StaticText(self, -1, "kW", (1105, 60)) wx.StaticText(self, -1, "Park Possible Capability", (920, 300)) wx.StaticText(self, -1, "Site Power", (920, 320)) wx.StaticText(self, -1, "kW", (1105, 300)) wx.StaticText(self, -1, "kW", (1105, 320)) title_Accu = wx.StaticText(self, -1, " Accumulated Monthly Counters ", (30, 372)) title_Accu.SetForegroundColour((0, 0, 255)) def retrieve_data_fromdb(self): # SQL Server configuration server = 'localhost' db = 'DB_NAME' user = 'user_name' pwd = 'password' 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() def OnTimer(self,event): a = self.retrieve_data_fromdb() self.wind_spd_val.SetLabel(a[0]) self.site_pwr_val.SetLabel(a[1]) self.acv_pr_setpnt_val.SetLabel(a[2]) self.park_pbl_cap_val.SetLabel(a[3]) self.tol_cur_val.SetLabel(a[4]) self.tol_non_prod_val.SetLabel(a[5]) 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 x2 = 20 y2 = 230 w2 = 500 h2 = 130 x3 = 550 y3 = 30 w3 = 580 h3 = 330 x4 = 20 y4 = 380 w4 = 1110 h4 = 150 radius = 3 gc.DrawRoundedRectangle(x1, y1, w1, h1, radius) gc.DrawRoundedRectangle(x2, y2, w2, h2, radius) gc.DrawRoundedRectangle(x3, y3, w3, h3, radius) gc.DrawRoundedRectangle(x4, y4, w4, h4, radius) pdc.DrawLine(770, 60, 820, 60) pdc.DrawLine(795, 60, 795, 100) pdc.DrawLine(795, 100, 810, 110) pdc.DrawLine(810, 110, 785, 130) pdc.DrawLine(785, 130, 795, 140) pdc.DrawLine(795, 140, 795, 160) pdc.DrawEllipse(775, 160, 40, 25) pdc.DrawEllipse(775, 170, 40, 25) pdc.DrawLine(795, 195, 795, 220) pdc.DrawLine(710, 220, 880, 220) pdc.DrawLine(710, 220, 710, 240) pdc.DrawLine(766.7, 220, 766.7, 240) pdc.DrawLine(823.3, 220, 823.3, 240) pdc.DrawLine(880, 220, 880, 240) image = wx.Image(r'C:\Haiyang\Demos\turbine.jpg',wx.BITMAP_TYPE_JPEG) portion = 0.06 w = image.GetWidth()*portion h = image.GetHeight()*portion image.Rescale(w,h) mypic = image.ConvertToBitmap() wx.StaticBitmap(self, -1, bitmap=mypic, pos=(703, 243)) wx.StaticBitmap(self, -1, bitmap=mypic, pos=(759.7, 243)) wx.StaticBitmap(self, -1, bitmap=mypic, pos=(816.3, 243)) wx.StaticBitmap(self, -1, bitmap=mypic, pos=(873, 243)) # grid = wx.grid.Grid(self) # grid.CreateGrid(len(data), len(data[0])) # for r in range(len(data)): # for c in range(len(data[r])): # grid.SetColLabelValue(c, column_names[c]) # grid.SetCellValue(r, c, data[r][c]) # # 设置行和列自定调整 # grid.AutoSize() # return gridex = wx.App()Mywin(None,'Real time tool')ex.MainLoop()
标签: self
②文章观点仅代表原作者本人不代表本站立场,并不完全代表本站赞同其观点和对其真实性负责。
③文章版权归原作者所有,部分转载文章仅为传播更多信息、受益服务用户之目的,如信息标记有误,请联系站长修正。
④本站一律禁止以任何方式发布或转载任何违法违规的相关信息,如发现本站上有涉嫌侵权/违规及任何不妥的内容,请第一时间反馈。发送邮件到 88667178@qq.com,经核实立即修正或删除。