【问题标题】:Python Tkinter start and stop button for a loop. I need a start and stop button for a loopPython Tkinter 循环的开始和停止按钮。我需要一个循环的开始和停止按钮
【发布时间】:2014-01-21 19:37:45
【问题描述】:

这是我希望 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()

【问题讨论】:

    标签: python loops button tkinter


    【解决方案1】:

    flash_1 是一个永不结束的函数。因此,它永远不会将程序流控制返回到主事件循环。没有事件循环 GUI 不会刷新和冻结。似乎是一个类似的问题:Python advice using delays with tkinter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2021-03-22
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多