Python-Main()循环不会重复执行

时间:2019-12-12 04:14:38

标签: python raspberry-pi3

(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()

1 个答案:

答案 0 :(得分:0)

尝试将其放入程序中

if __name__ == '__main__':
    main()