我是Python的新手,并尝试使用Raspberry在我的RPI中制作一个小脚本。 我的问题是代码在 Thonny 中运行良好,但是当我在 LXTerminal 中加载它时,某些函数无法正常工作。
当我在 Thonny 中进行计算并按下 button21 30 秒时,有效(round((有效时间/刷新(*100((值将为 50%,但当我在终端中有效执行相同操作时,将为 0,但如果我根本不按 button21,则有效值将为 100。
这就像有效计时器只是发送"全部价值"或什么都不发送。
有人有想法吗?
!/usr/bin/env python
import pyodbc
import time
from multiprocessing import Process, Value
from gpiozero import Button
from threading import Thread
button21 = Button(21)
button20 = Button(20)
cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER=****;PORT=1433;DATABASE=****;UID=****;PWD=****;TDS_Version=8.7;')
counter=Value('h', 0)
x=Value('h', 0)
refresh = 10
elapsed_stoptime = 0
effectivelytime = 1
effectively = 0
precounter = 0
def function1():
while True:
global effectivelytime
global effectively
global precounter
effectivelytime = x.value
effectively = round((effectivelytime / refresh)*100)
if counter.value == precounter:
effectively = 0
cursor = cnxn.cursor()
cursor.execute('INSERT INTO ****.dbo.****(counter, effectively) VALUES ({},{})'.format (counter.value, effectively,))
cnxn.commit()
cursor.close()
print("Printed To DB - Counter = ", counter.value, ", Effectively = ", effectively,)
x.value = 0
precounter = counter.value
time.sleep(refresh)
def function2():
button21.wait_for_release()
counter.value = counter.value + 1
print (counter.value, "Cykel")
def effectivelytimer():
while True:
if button20.is_pressed:
x.value = x.value + 1
time.sleep(1)
else:
time.sleep(1)
def trigcounter():
t2 = Thread(group=None,target=function2)
t2.start()
button21.when_pressed = trigcounter
if __name__ == '__main__':
t1 = Thread(group=None,target=function1)
t3 = Thread(group=None,target=effectivelytimer)
t1.start()
t3.start()
我根本没有收到任何错误消息。
Thonny 使用 Python 3。脚本的 shebang 行 (!/usr/bin/env python
( 指的是 Python 2。尝试将!/usr/bin/env python
更改为!/usr/bin/env python3