python - Python - Main() 循环不重复执行
问题描述
(Python新手,尽我所能)
该程序运行,并且按钮执行正确的操作。但是 MAIN() 过程块不起作用。我希望 main() 继续循环,直到用户关闭程序。在循环中是一个关于时间比较的简单决定,如果 10 秒过去了,3 个按钮将获得浅灰色背景。
我在 main() 区域尝试了一些打印语句,但它们根本不打印。我哪里错了?
谢谢一堆
from guizero import App, Text, TextBox, PushButton
import RPi.GPIO as GPIO
import sys
import time
import datetime
from datetime import timedelta
from datetime import datetime
# This procedure will watch three inputs and lock in the team that calls in first for 5seconds
# Change the colors of the buttons to show which team pressed in first.
def action1(dunno):
# Team1 button pressed
global SetCurrTime
if datetime.now() > SetCurrTime + timedelta(seconds=5):
# Mark Team 1 as the buzzer beater
team1_btn.bg="green"
team2_btn.bg="red"
team3_btn.bg="red"
SetCurrTime = datetime.now()
txtButtons_Hit.value = "Team1 was hit: " + str(SetCurrTime)
def action2(dunno):
# Team2 button pressed
global SetCurrTime
if datetime.now() > SetCurrTime + timedelta(seconds=5):
# Mark Team 2 as the buzzer beater
team1_btn.bg="red"
team2_btn.bg="green"
team3_btn.bg="red"
SetCurrTime = datetime.now()
txtButtons_Hit.value = "Team2 was hit: " + str(SetCurrTime)
def action3(dunno):
# Team3 button pressed
global SetCurrTime
if datetime.now() > SetCurrTime + timedelta(seconds=5):
# Mark Team 3 as the buzzer beater
team1_btn.bg="red"
team2_btn.bg="red"
team3_btn.bg="green"
SetCurrTime = datetime.now()
txtButtons_Hit.value = "Team3 was hit: " + str(SetCurrTime)
def main():
#reset board when 5seconds has elapsed from SetCurrTime
app.display()
global SetCurrTime
print ("Here")
while True:
txtButtons_Hit.value = "Current Time: " + str(datetime.now())
print ("here")
if datetime.now() > SetCurrTime + timedelta(seconds=5):
# clear the board
team1_btn.bg="light grey"
team2_btn.bg="light grey"
team3_btn.bg="light grey"
# Pin setup
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(16, GPIO.RISING, callback=action1, bouncetime=1800)
GPIO.add_event_detect(20, GPIO.RISING, callback=action2, bouncetime=1800)
GPIO.add_event_detect(21, GPIO.RISING, callback=action3, bouncetime=1800)
SetCurrTime = datetime.now() + timedelta(seconds=-5) #init this at the start of the program
app = App(title="A07 DSS Tri-State - Jeopardy", width=700)
blank1 = Text(app, text="")
welcome_message = Text(app, text="This.. is.. Jeopardy!!", size=30, font="Times New Roman", color="blue")
blank2 = Text(app, text="")
blank3 = Text(app, text="")
team1_btn = PushButton(app, text="Team Black & Blue", command = lambda:action1(1))
team1_btn.bg="light grey"
blank4 = Text(app, text="")
team2_btn = PushButton(app, text="Team Holiday Spirit", command=lambda:action2(2))
team2_btn.bg="light grey"
blank5 = Text(app, text="")
team3_btn = PushButton(app, text="Team Creamsicle", command=lambda:action3(3))
team3_btn.bg="light grey"
blank6 = Text(app, text="")
delay_timer = Text(app, text="0")
delay_timer.visible=False
blank7 = Text(app, text="")
txtButtons_Hit = Text(app, text="Current Time: " + str(SetCurrTime))
main()
解决方案
试着把它放在程序中
if __name__ == '__main__':
main()
推荐阅读
- node.js - NodeJS GCP pubsub 发布错误:t.topic(...).publish 不是函数
- c++ - 用于旋转网格的 OpenGL ArcBall
- ms-word - 如何捕获任务窗格中动态内容的点击事件?
- jquery - jQuery 拖放模拟对最后一个可拖动对象不起作用
- autohotkey - 未从字典中提取数字的值
- python - 什么是 NameError,我该如何解决?
- angular - 更新我要显示的项目列表时,使用 ngFor 动态添加组件不起作用
- javascript - 如何使用 Google Maps JavaScript API 请求从当前位置到目的地的路线
- python - 组合熊猫中的奇数和偶数索引行
- python - 多个异步读取器和单个写入器(不同线程)的效率