这是我希望4个LED开启然后相互关闭的代码,但是可以通过GUI开始和停止按钮关闭和打开它们。我是python和编码的新手。我环顾四周,似乎无法找到解决问题的方法。
from Tkinter import *
import RPi.GPIO as GPIO
import sys
import time
##Setup
#GPIO
GPIO.setmode(GPIO.BOARD)
#Window
led = Tk()
led.title("LED Controller")
led.geometry("345x200+385+350")
#Title
title = Label(text="My LED Controller",fg="purple")
title.pack()
#Pin 7 = Front Left
GPIO.setup(7,GPIO.OUT)
GPIO.output(7, GPIO.LOW)
#Pin 11 = Front Right
GPIO.setup(11,GPIO.OUT)
GPIO.output(11, GPIO.LOW)
#Pin 13 = Back Left
GPIO.setup(13,GPIO.OUT)
GPIO.output(13, GPIO.LOW)
#Pin 15 = Back Right
GPIO.setup(15,GPIO.OUT)
GPIO.output(15, GPIO.LOW)
###Flash
##Define Loop/Flashing
#Flash
def flash_1():
while True:
if flashButton_1:
GPIO.output(7, GPIO.HIGH)
print "Pin 7 High"
time.sleep(1)
GPIO.output(7, GPIO.LOW)
print "Pin 7 Low"
GPIO.output(11, GPIO.HIGH)
print "Pin 11 High"
time.sleep(1)
GPIO.output(11, GPIO.LOW)
print "Pin 11 Low"
GPIO.output(13, GPIO.HIGH)
print "Pin 13 High"
time.sleep(1)
GPIO.output(13, GPIO.LOW)
print "Pin 13 Low"
GPIO.output(15, GPIO.HIGH)
print "Pin 15 High"
time.sleep(1)
GPIO.output(15, GPIO.LOW)
print "Pin 15 Low"
###Pack/Placement of buttons
##Flash
#Flash 1 Start
flashButton_1 = Button(led, text="Flash On", fg="red", command=flash_1)
flashButton_1.place(x=5,y=100)
#Loop Command
led.mainloop()
#Cleanup Command
GPIO.cleanup()
答案 0 :(得分:1)
flash_1
是一个永无止境的功能。因此,它永远不会将程序流控制返回到主事件循环。没有事件循环GUI不会刷新和冻结。这似乎是一个类似的问题:Python advice using delays with tkinter