创建一个Python窗口,使用RPi读取的值动态更新?

时间:2017-07-06 13:58:34

标签: python user-interface window subprocess real-time-data

新手说到python,我需要帮助创建一个基本窗口,打印和更新我根据RPi收集的读数更改的变量。基于电压是> 1.0还是<1.0,窗口将显示ON或OFF。到目前为止,变量'values'只是打印在终端中。 到目前为止我的代码:

    import time
import spidev
import os
import sys
from windowex import *
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

# Software SPI configuration:
CLK  = 12
MISO = 17
MOSI = 27
CS   = 22
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

x0 = "OFF"
x1 = "OFF"
x2 = "OFF"
x3 = "OFF"
x4 = "OFF"
x5 = "OFF"
x6 = "OFF"
x7 = "OFF"

y0 = "ON"
y1 = "ON"
y2 = "ON"
y3 = "ON"
y4 = "ON"
y5 = "ON"
y6 = "ON"
y7 = "ON"

# Main program loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7). rounds to nearest 2 decimals
        values[i] = round((mcp.read_adc(i)*(3.3/1023)),2)

#conditional statements for ON and OFF renaming        
    if values[0] < 1.0:
         values[0] = x0   
    else:
         values[0] = y0

    if values[1] < 1.0:
         values[1] = x1     
    else:
         values[1] = y1

    if values[2] < 1.0:
         values[2] = x2      
    else:
         values[2] = y2

    if values[3] < 1.0:
         values[3] = x3      
    else:
         values[3] = y3

    if values[4] < 1.0:
         values[4] = x4       
    else:
         values[4] = y4

    if values[5] < 1.0:
         values[5] = x5     
    else:
         values[5] = y5

    if values[6] < 1.0:
         values[6] = x6       
    else:
         values[6] = y6

    if values[7] < 1.0:
         values[7] = x7
    else:
         values[7] = y7  


    print values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7]

    time.sleep(0.5)

1 个答案:

答案 0 :(得分:0)

while True:
    # Read all the ADC channel values in a list.
    values = [0]*8
    output = ""
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7). rounds to nearest 2 decimals
        values[i] = round((mcp.read_adc(i)*(3.3/1023)),2)
        output += "{}-{} ".format(i, "OFF" if values[i] < 1.0 else "ON" )
    print output
    time.sleep(0.5)


>> "0-ON 1-OFF 2-ON 3-OFF ..."

为什么稍后将值更改为字符串,只要它是您想要“ON / OFF”的范围。所有x0,x1,...都是相同的字符串,为什么需要定义呢?