简体   繁体   English

Raspberry Pi GPIO引脚控制Tkinter GUI秒表

[英]Raspberry pi GPIO pins to control Tkinter GUI stopwatch

below are the codes creating Buttons on the GUI to control the stopwatch. 以下是在GUI上创建按钮以控制秒表的代码。 I would like to ask if anyone knows how to modified the code in the way such that we can use GPIO pins as input on the raspberry PI (meaning we have 3 push button components to control the stopwatch to function). 我想问问是否有人知道如何修改代码,以便我们可以将GPIO引脚用作树莓派PI的输入(这意味着我们有3个按钮组件来控制秒表的功能)。

What i only know is that we must import RPi.GPIO as GPIO , GPIO.setmode(GPIO.BOARD) and GPIO.setup() the GPIO pins. 我只知道,我们必须将RPi.GPIO导入为GPIO,将GPIO.GPIO.setmode(GPIO.BOARD)和GPIO.setup()导入GPIO引脚。 Anybody can help me??? 任何人都可以帮助我吗?

from Tkinter import *
import time

class StopWatch(Frame):  
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):        
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()      

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr)
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=2, padx=2)                      

    def _update(self): 
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(50, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)                
        self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))

    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1        

    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        if self._running:
            self.after_cancel(self._timer)            
            self._elapsedtime = time.time() - self._start    
            self._setTime(self._elapsedtime)
            self._running = 0

    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)

def main():

    root = Tk()
    sw = StopWatch(root)
    sw.pack(side=TOP)

    Button(root, text='Start', command=sw.Start).pack(side=LEFT)
    Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
    Button(root, text='Quit', command=root.quit).pack(side=LEFT)

    root.mainloop()

if __name__ == '__main__':
    main()

Makezine propose an extensive tutorial of GPIO use. Makezine提出有关GPIO使用的广泛教程

You could poll buttons values in your _update method. 您可以在_update方法中轮询按钮值。

if self._running and (GPIO.input(23) ==1):
    self.Stop()

This will not work when your clock is not running, so you might adapt your logic to have your _update after-loop to be always running (or create another after-loop dedicated to Pi buttons polling). 当您的时钟未运行时,这将不起作用,因此您可以调整逻辑以使_update后循环始终运行(或创建另一个专用于Pi按钮轮询的后循环)。

Also, GPIO provide a waitloop in another thread. 同样,GPIO在另一个线程中提供一个waitloop。 Here is an adaptation of Makezine's example to link back to tkinter (not tested). 这是Makezine的示例的改编版本,可链接回tkinter(未经测试)。

def relayToTkinter(channel):
    sw.event_generate('<<Start>>', when='tail')

GPIO.add_event_detect(23, GPIO.RISING, callback=relayToTkinter, bouncetime=300)
sw.bind("<<Start>>", lambda event:sw.Start())

event_generate("<<VirtualEvent>>", when='tail') is a safe way to have another thread interact with main UI thread. event_generate("<<VirtualEvent>>", when='tail')是让另一个线程与主UI线程进行交互的一种安全方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM