Python - 使用函数打破while循环(Raspberry Pi)

时间:2016-07-03 14:40:57

标签: python raspberry-pi

我有一个涉及树莓派,dothat 16x2 lcd显示器和一些python代码的项目。我本质上是试图创建一个动态的while循环,显示有关lcd的信息。我还试图通过按下其中一个触摸按钮来添加取消while循环的功能(参考:https://github.com/pimoroni/dot3k/blob/master/python/REFERENCE.md

这是我到目前为止所得到的:

import dothat.lcd as l
import dothat.backlight as b
import dothat.touch as t
from time import sleep
import signal
import os

def main():
     i=0
     k=0
     while True:
          l.clear()                   # Clear LCD screen
          b.hue(1.5)                  # Set background color
          l.set_cursor_position(0, 1) # Set cursor position on LCD
          l.write("%s" % k)           # Write variable "k" to LCD
          @t.on(t.CANCEL)             # When CANCEL button is pressed then go to function
          def cancel(ch, evt):        
               i=1                    # Set variable "i" as 1
               return
          if i == 1:                  
               break
          k=k+1
          sleep(1)
     l.clear()                         # Clear LCD screen
     b.off()                           # Turn the LCD Backlight off
     cmd='pkill python'                # 
     os(cmd)                           # Kill all python processes
     signal.pause()                    
main()                                 

while循环正在运行但按下按钮时不会中断。想法?

2 个答案:

答案 0 :(得分:0)

我修好了,虽然我收到了关于'模块的错误。关于os(cmd)的对象不可调用。

代码:

def main():
     global i
     i=0
     k=0
     while True:
          l.clear()
          b.hue(1.5)
          l.set_cursor_position(0, 1)
          l.write("%s" % k)
          @t.on(t.CANCEL)
          def cancel(ch, evt):
               global i
               i=1
               return
          if i == 1:
               break
          k=k+1
          sleep(1)
     l.clear()
     b.off()
     cmd='pkill python'
     os(cmd)
     signal.pause()
main()

答案 1 :(得分:-1)

我没有dothat LCD显示屏,因此我无法测试您的代码。但我认为@ Pigface333是正确的,i内的cancel是一个局部变量,因此在按下取消后,i语句中的if不会设置为1。以下代码演示了:

from time import sleep

def main():
    i = 0
    k = 0
    while True:
        def cancel():
            print "inside cancel"
            i = 1
            return
        cancel()
        if i == 1:
            break
        k = k+1
        sleep(1)
    exit(0)
main()

这将每1秒打印inside cancel,但不会退出,表示i内的cancel是本地变量。要修复它,您可以创建一个存储取消状态的类:

from time import sleep

class Cancel(object):

    def __init__(self):
        self.is_cancelled = False

    def cancel(self):
        self.is_cancelled = True


def main():
    canceller = Cancel()
    while True:
        canceller.cancel()
        if canceller.is_cancelled:
            break
        sleep(1)
    exit(0)
main()

同样的方法可以应用于您的代码:

import dothat.lcd as l
import dothat.touch as t
import dothat.backlight as b
from time import sleep
import signal


class Cancel(object):

    def __init__(self,):
        self.is_cancelled = False

    @t.on(t.CANCEL)
    def cancel(self, ch, evt):
        self.is_cancelled = True
        return


def main():
    k = 0
    cancel = Cancel()
    while True:
        l.clear()                    # Clear LCD screen
        b.hue(1.5)                   # Set background color
        l.set_cursor_position(0, 1)  # Set cursor position on LCD
        l.write("%s" % k)            # Write variable "k" to LCD
        if cancel.is_cancelled:
            break
        k = k+1
        sleep(1)
    l.clear()                         # Clear LCD screen
    b.off()                           # Turn the LCD Backlight off
    signal.pause()
    exit(0)
main()

为了帮助理解为什么原始代码不起作用以及为什么使用类是一个好主意,我建议阅读Python variable's scopeObject-Oriented Prograaming in Python