1
我有一個從mcp3008和雨水傳感器獲取數據的python程序。我想使用wxpython在gui中顯示它。這是我的傳感器程序:如何使用mcp3008的數據顯示和動態更新多個wxpython靜態文本?
import spidev
from time import sleep
import os
spi = spidev.SpiDev()
spi.open(0,0)
def getAdc (channel):
if ((channel>7)or(channel<0)):
return -1
r = spi.xfer2([1, (8+channel) << 4, 0])
adcOut = ((r[1]&3) << 8) + r[2]
percent = int(round(adcOut/10.24))
volts = ((adcOut/1023) * 5)
if adcOut >= 0 and adcOut <= 300:
print "--------------------------------------------------------------"
print ("ADC Output: {0:4d} Percentage: {1:3}% Voltage : {2} V".format(adcOut,percent,volts))
print ("Rain Condition : Heavy Rain")
sleep(5)
elif adcOut >= 0 and adcOut <= 500:
print "--------------------------------------------------------------"
print ("ADC Output: {0:4d} Percentage: {1:3}% Voltage : {2} V".format(adcOut,percent,volts))
print ("Rain Condition : Moderate Rain")
sleep(5)
elif adcOut >= 0 and adcOut <= 700:
print "--------------------------------------------------------------"
print ("ADC Output: {0:4d} Percentage: {1:3}% Voltage : {2} V".format(adcOut,percent,volts))
print ("Rain Condition : Light Rain")
sleep(5)
else :
print "--------------------------------------------------------------"
print ("ADC Output: {0:4d} Percentage: {1:3}% Voltage : {2} V".format(adcOut,percent,volts))
print ("Rain Condition : No Rain")
sleep(5)
while True:
getAdc(0)
這裏是我創建的wxpython程序來顯示它。幫助我如何將兩個程序合併爲一個來顯示數據。
import datetime
global current_time
current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
try:
import wx
except ImportError:
raise ImportError, "The wxPython module is required to run this program."
class RainSensorApp_wx(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, size = (500, 300))
self.SetBackgroundColour(wx.BLUE)
self.parent = parent
self.initialize()
def initialize(self):
sizer = wx.GridBagSizer()
font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
self.SetFont(font)
self.label = wx.StaticText(self, -1, label = u'Rain Sensor Level: {0:4d} Percentage: {1:3}% Voltage: {2} V'.format(adcOut, percent, volts))
self.label.SetBackgroundColour(wx.BLUE)
self.label.SetForegroundColour(wx.WHITE)
sizer.Add(self.label, (1,0), (1,2), wx.EXPAND)
self.label = wx.StaticText(self, -1, label = u'Rain Condition: {}'.format(rain_condition))
self.label.SetBackgroundColour(wx.BLUE)
self.label.SetForegroundColour(wx.WHITE)
sizer.Add(self.label, (2,0), (1,3), wx.EXPAND)
self.label = wx.StaticText(self, -1, label = u'Time Updated: {}'.format(current_time))
self.label.SetBackgroundColour(wx.BLUE)
self.label.SetForegroundColour(wx.WHITE)
sizer.Add(self.label, (3,0), (1,4), wx.EXPAND)
self.SetSizer(sizer)
self.Show(True)
def on_timer(self):
wx.CallLater(1000, self.on_timer)
if __name__ == "__main__":
app = wx.App()
frame = RainSensorApp_wx(None, -1, 'Rain Sensor Monitor')
app.Mainloop()
getAdc(0)
在此之後,我將使用使用callLater動態更新多個wxPython的靜態文本,因爲我學就昨天增加計時器。我很感激誰會幫助我並閱讀我的文章。
感謝您的回答。這個星期二我會解決你的問題,並在測試之後給出更新。如果任何人有其他答案,我歡迎它作爲更多的解決方案,我會得到更成功的答案。 – anubismmt
我已經嘗試了你的代碼,它在除了在gui程序啓動之前在python 2.7.10外殼上發出(SyntaxWarning:名稱'adcOut'被分配給全局聲明之前)之外的工作。我還對伏特代碼進行了小改動,以小數點後兩位顯示,並在全局current_time中添加self.label3.SetLabel(「Time Updated:{}」。format(current_time)並初始化current_time,因爲您忘記了更新時間 – anubismmt
正如答案中所述,「足以讓你開始」,這是一個駭人聽聞的事情,以解決我無法訪問「mcp3008和雨水傳感器」的事實。格式和時間問題,你原來的代碼沒有包括它們,我不是一個心靈讀者。除此之外,你是最歡迎的。 –