Raspberry Pi GPIO引脚控制Tkinter GUI秒表 [英] Raspberry pi GPIO pins to control Tkinter GUI stopwatch

查看:124
本文介绍了Raspberry Pi GPIO引脚控制Tkinter GUI秒表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是在GUI上创建按钮以控制秒表的代码. 我想问问是否有人知道如何修改代码,以便我们可以将GPIO引脚用作树莓派PI上的输入(这意味着我们有3个按钮组件来控制秒表的功能).

below are the codes creating Buttons on the GUI to control the stopwatch. 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).

我只知道我们必须将RPi.GPIO导入为GPIO,将GPIO.GPIO.setmode(GPIO.BOARD)和GPIO.setup()导入GPIO引脚. 任何人都可以帮助我吗?

What i only know is that we must import RPi.GPIO as GPIO , GPIO.setmode(GPIO.BOARD) and GPIO.setup() the GPIO pins. 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提出有关GPIO使用的广泛教程.

Makezine propose an extensive tutorial of GPIO use.

您可以在_update方法中轮询按钮值.

You could poll buttons values in your _update method.

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

这在时钟不运行时不起作用,因此您可以调整逻辑以使_update后循环始终运行(或创建另一个专门用于Pi按钮轮询的后循环).

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

此外,GPIO在另一个线程中提供了一个waitloop.这是Makezine的示例的改编版本,可链接回tkinter(未经测试).

Also, GPIO provide a waitloop in another thread. Here is an adaptation of Makezine's example to link back to tkinter (not tested).

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')是让另一个线程与主UI线程交互的一种安全方法.

event_generate("<<VirtualEvent>>", when='tail') is a safe way to have another thread interact with main UI thread.

这篇关于Raspberry Pi GPIO引脚控制Tkinter GUI秒表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆