如何使用TKinter在树莓派PI上运行无限循环?

时间:2018-12-05 04:42:32

标签: python tkinter raspberry-pi

我一直在从事一个项目,在这个项目中,我必须制作一个可以根据用户设置的温度和都由树莓派控制的传感器来控制冷却模块的GUI。但是,我无法在TKinter旁边制作连续运行的代码,同时无法适应用户的幻灯片输入。我设想该代码将允许用户输入温度,同时每10秒显示一次从热传感器上读取的温度,然后移动幻灯片以设置温度并启动系统。在过去的几天里,我一直在努力找出这些问题,希望我在这里的人能指出正确的方向。

这是我的代码,

import board
import busio
import adafruit_si7021
import time
import RPi.GPIO as gpio
from tkinter import *
import RPi.GPIO as GPIO
import time
import sys
from hx711 import HX711
import threading
import queue

# Force Python 3         ###########################################################

if sys.version_info[0] != 3:
raise Exception("Python 3 is required.")

#Set up
i2c = busio.I2C(board.SCL, board.SDA);
sensor = adafruit_si7021.SI7021(i2c);
hx = HX711(5, 6)

gpio.setmode(gpio.BCM)
gpio.setup(23, gpio.IN, pull_up_down=GPIO.PUD_UP)
gpio.setup(17, gpio.OUT)
gpio.setup(22, gpio.OUT)

#Function Call

#User Temperature
def usertemp(usertemp):
ftemp=int(usertemp)
usertempf=(int(ftemp-32)*5/9)
print(usertempf)
#print(temp)
temp.after(100,usertemp.usertemp)



#Turn on TEC
def turnon():
gpio.output(17, True)
gpio.output(22, True)

#Turn off TEC
def turnoff():
gpio.output(17, False)
gpio.output(22, False)

#weight sensor
def cleanAndExit():
print("Cleaning...")
GPIO.cleanup()
print("Bye!")
sys.exit()

def setup():

code run once

hx.set_offset(8468428.9375)
hx.set_scale(-136.28067885117494)

#loop reading weight
def loop():



try:
val = hx.get_grams()
print(val)

hx.power_down()
time.sleep(.001)
hx.power_up()

time.sleep(2)
except (KeyboardInterrupt, SystemExit):
cleanAndExit()
#GUI
root = Tk()
Root for gui
boxxle = Label(root, text="Boxxle Wine Chiller", font="times 35", fg="red")
whiter = Label(root)
tslide = Label(root, text="Set Temperature", font="times 12", fg="black")
temperature = Label(root, text="Current Temperature\n73         Degrees F", font="times 12", fg="black")
left = Label(root, text="Percent Left is XX%", font="times 12", fg="black")

#slide
slide1=Scale(root, from_=72, to=35, command=usertemp)

#GUI placement 
boxxle.grid(row=0, sticky=W)
tslide.grid(row=2, column=9)
boxxle.grid(row=0, column=1)
whiter.grid(row=1, column=8)
temperature.grid(row=2, column=0)
slide1.grid(row=2, column=8)
left.grid(row=7, column=1)

root.mainloop()


#Set up loop for magnetic sensor
setup()
weight = loop()+10

while True:
loop()

    if GPIO.input(23)==True:
    pumpdown()
print("door is open")
time.sleep(10)
break

if loop(val) <> weight:
pumpup()
else:
pumpupoff()

if sensor.temperature+3 > usertempf or sensor.temperature--3 > usertempf:
turnon()
else:
turnoff()

0 个答案:

没有答案