如何使用mcp3008中的数据显示和动态更新多个wxpython静态文本?

如何使用mcp3008中的数据显示和动态更新多个wxpython静态文本?,python,wxpython,Python,Wxpython,我有一个python程序,可以从mcp3008和雨水传感器获取数据。我想使用wxpython在gui中显示它。这是我的传感器程序: 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

我有一个python程序,可以从mcp3008和雨水传感器获取数据。我想使用wxpython在gui中显示它。这是我的传感器程序:

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)

在此之后,我将使用CallLater添加计时器,动态更新多个wxpython静态文本,正如我昨天刚刚了解的那样。感谢有人帮助我阅读我的文章。

这有点像黑客,因为我必须模仿spidev,但这应该足以让你开始。
在我认为重要的地方记录代码

import datetime
#import spidev
from time import sleep
import os

#spi = spidev.SpiDev()
#spi.open(0,0)
#Setup some variables as I don't have spidev
global adcOut
adcOut = 200
percent = int(round(adcOut/10.24))
volts = ((adcOut/1023) * 5)
rain_condition="none"

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.label1 = wx.StaticText(self, -1, label = u'Rain Sensor Level:   {0:4d}  Percentage:     {1:3}%  Voltage:    {2} V'.format(adcOut, percent, volts))

        #Give the labels unique names

        self.label1.SetBackgroundColour(wx.BLUE)
        self.label1.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND)

        self.label2 = wx.StaticText(self, -1, label = u'Rain Condition:  {}'.format(rain_condition))
        self.label2.SetBackgroundColour(wx.BLUE)
        self.label2.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND)

        self.label3 = wx.StaticText(self, -1, label = u'Time Updated: {}'.format(current_time))
        self.label3.SetBackgroundColour(wx.BLUE)
        self.label3.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND)

    #Create a timer and perform on_timer every 1000 milliseconds(change to 5000)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
        self.timer.Start(1000)

        self.SetSizer(sizer)
        self.Show(True)

    def on_timer(self,event):
        channel = 0
        if ((channel>7)or(channel<0)):
            return -1
    #Hack out spidev references and use globals to emulate something happening

#        r = spi.xfer2([1, (8+channel) << 4, 0])
#        adcOut = ((r[1]&3) << 8) + r[2]

        global adcOut
        adcOut = adcOut +1
        percent = int(round(adcOut/10.24))
        volts = ((adcOut/1023) * 5)
        if adcOut >= 0 and adcOut <= 300:

        # Update the screen output 

                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Heavy Rain")

        elif adcOut >= 0 and adcOut <= 500:
                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Moderate Rain")

        elif adcOut >= 0 and adcOut <= 700:
                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Light Rain")

        else :
                self.lable1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.lable2.SetLabel("Rain Condition : No Rain")

if __name__ == "__main__":
    Rs = wx.App()
    RainSensorApp_wx(None, -1, 'Rain Sensor Monitor')
    Rs.MainLoop()
导入日期时间
#导入spidev
从时间上导入睡眠
导入操作系统
#spi=spidev.spidev()
#spi.开启(0,0)
#设置一些变量,因为我没有spidev
全球adcOut
adcOut=200
百分比=整数(四舍五入(adcOut/10.24))
电压=((adcOut/1023)*5)
雨水条件=“无”
全局当前时间
当前时间=datetime.datetime.strftime(datetime.datetime.now(),“%d-%m-%y%H:%m:%S”)
尝试:
导入wx
除恐怖外:
“运行此程序需要wxPython模块。”
类雨量传感器APP_wx(wx.框架):
定义初始化(自我、父项、id、标题):
wx.Frame.\uuuuu init\uuuuuu(self,parent,id,size=(500300))
self.setbackgroundColor(蓝色)
self.parent=parent
self.initialize()
def初始化(自):
sizer=wx.GridBagSizer()
font=wx.font(20,wx.DECORATIVE,wx.ITALIC,wx.NORMAL)
self.SetFont(字体)
self.label1=wx.StaticText(self,-1,label=u'Rain Sensor Level:{0:4d}百分比:{1:3}%电压:{2}V'。格式(adcOut,百分比,伏特))
#为标签指定唯一的名称
self.label1.setbackgroundColor(wx.BLUE)
自标签1.SetForeGroundColor(宽x.白色)
sizer.Add(self.label1,(1,0),(1,2),wx.EXPAND)
self.label2=wx.StaticText(self,-1,label=u'Rain条件:{}'。格式(Rain_条件))
self.label2.setbackgroundColor(wx.BLUE)
self.label2.setForeGroundColor(宽x.白色)
sizer.Add(self.label2,(2,0)、(1,3),wx.EXPAND)
self.label3=wx.StaticText(self,-1,label=u'时间更新:{}'。格式(当前时间))
self.label3.setbackgroundColor(wx.BLUE)
self.label3.setForeGroundColor(宽x.白色)
sizer.Add(self.label3,(3,0),(1,4),wx.EXPAND)
#创建计时器并每1000毫秒执行一次计时器(更改为5000)
self.timer=wx.timer(self)
self.Bind(wx.EVT_定时器、self.on_定时器、self.TIMER)
自动定时器启动(1000)
自整定器(施胶器)
自我展示(真实)
def on_定时器(自身、事件):
通道=0

如果((通道>7)或(通道)谢谢你的回答。我将在本周二公布你的解决方案,并在测试后提供更新。如果有人有其他答案,我欢迎它,因为我得到的解决方案越多,我会得到的答案就越成功。我已经尝试了你的代码,它正在工作,只是没有给出答案(SyntaxWarning:name'adcOut'是在全局声明之前分配给的)在python 2.7.10 shell上,在gui程序启动之前。我还对volts代码做了一些小改动,以两位小数显示它,并添加self.label3.SetLabel(“时间更新:{}.”格式(当前_时间)使用全局当前_时间并初始化当前_时间,因为您忘记了更新答案中所述的时间,“足以让您开始”。这有点像黑客,因为我无法访问“mcp3008和雨水传感器”。至于更新格式和时间问题,您的原始代码没有包含它们,我也不是一个读心术的人。除此之外,非常欢迎您。很抱歉没有语法错误。它显示出来了,因为我在计时器代码中将#放在全局adcOut前面。但是我有一个问题,为什么您将adcOut=adcOut+1放在“全局adcOut”下面。@anubismmt这只是为了模拟一个不断变化的电压,因为我没有传感器。我用它来测试“降雨条件”随时间的变化。这对你的代码没有任何用处。
import datetime
#import spidev
from time import sleep
import os

#spi = spidev.SpiDev()
#spi.open(0,0)
#Setup some variables as I don't have spidev
global adcOut
adcOut = 200
percent = int(round(adcOut/10.24))
volts = ((adcOut/1023) * 5)
rain_condition="none"

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.label1 = wx.StaticText(self, -1, label = u'Rain Sensor Level:   {0:4d}  Percentage:     {1:3}%  Voltage:    {2} V'.format(adcOut, percent, volts))

        #Give the labels unique names

        self.label1.SetBackgroundColour(wx.BLUE)
        self.label1.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND)

        self.label2 = wx.StaticText(self, -1, label = u'Rain Condition:  {}'.format(rain_condition))
        self.label2.SetBackgroundColour(wx.BLUE)
        self.label2.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND)

        self.label3 = wx.StaticText(self, -1, label = u'Time Updated: {}'.format(current_time))
        self.label3.SetBackgroundColour(wx.BLUE)
        self.label3.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND)

    #Create a timer and perform on_timer every 1000 milliseconds(change to 5000)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
        self.timer.Start(1000)

        self.SetSizer(sizer)
        self.Show(True)

    def on_timer(self,event):
        channel = 0
        if ((channel>7)or(channel<0)):
            return -1
    #Hack out spidev references and use globals to emulate something happening

#        r = spi.xfer2([1, (8+channel) << 4, 0])
#        adcOut = ((r[1]&3) << 8) + r[2]

        global adcOut
        adcOut = adcOut +1
        percent = int(round(adcOut/10.24))
        volts = ((adcOut/1023) * 5)
        if adcOut >= 0 and adcOut <= 300:

        # Update the screen output 

                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Heavy Rain")

        elif adcOut >= 0 and adcOut <= 500:
                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Moderate Rain")

        elif adcOut >= 0 and adcOut <= 700:
                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Light Rain")

        else :
                self.lable1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.lable2.SetLabel("Rain Condition : No Rain")

if __name__ == "__main__":
    Rs = wx.App()
    RainSensorApp_wx(None, -1, 'Rain Sensor Monitor')
    Rs.MainLoop()