51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

为什么这些功能仍然处于活动状态?Python 3

英文:

Why are these functions still active? Python 3

问题 {#heading}

我正在尝试制作一个程序来输入莫尔斯电码到我的树莓派(Pi)。我有3个按钮,一个用于点号,一个用于短线,还有一个命令按钮,我想用它来指示字母的结束。我已经使用线程使树莓派监听来自这3个按钮的输入。然而,我也希望能够停止监听这些按钮。我尝试创建了一个全局变量 n,当按下命令按钮时设置为10,这应该会终止所有的 while 循环并结束线程,但是这并没有发生,n 确实被设置为10,但是由于某种原因,所有的 while 循环都在继续运行。似乎完全忽略了 n == 0 的条件,我做错了什么?

from gpiozero import Button
from threading import Thread
import sys
btn1 = Button(2)
btn2 = Button(3)
cmdbtn = Button(4)
currentMorse = ""

def pressed1():
    print("Pressed1")
    global currentMorse
    currentMorse = currentMorse + "."
    print(currentMorse)

def released1():
    print("Released")

def pressed2():
    print("Pressed2")
    global currentMorse
    currentMorse = currentMorse + ","
    print(currentMorse)

def released2():
    print("Released")

def pressedcmd():
    print("Pressedcmd")

def releasedcmd():
    print("Releasedcmd")
    global n
    n = 10

def checker1():
    global n
    while n == 0:
        btn1.when_pressed = pressed1
        btn1.when_released = released1
    print(n)

def checker2():
    global n
    while n == 0: 
        btn2.when_pressed = pressed2
        btn2.when_released = released2
    print(n)

def checkercmd():
    global n
    while n == 0:         
        cmdbtn.when_pressed = pressedcmd
        cmdbtn.when_released = releasedcmd
    print(n)

if __name__ == '__main__':
    n = 0
    Thread(target=checker1).start()
    Thread(target=checker2).start()
    Thread(target=checkercmd).start()

我已经尝试在其中加入系统退出以尝试终止线程,并尝试以各种方式修改 n 的条件,但是没有任何变化,它似乎仍然接受来自按钮的输入,就好像 n == 0 的条件不存在一样。 英文:

I'm trying to make a program to enter morse into my Pi, I have 3 buttons one for dot one for dash and a command button I want to use to indicate the end of a letter. I have used threads so that the Pi is listening for input from all 3 buttons. However I want to be able to make it stop listening to the buttons too. I've tried to make a global variable n which when the command button is pushed it sets to 10 which should break all the while-loops and end the threads - however this is not happening, n is indeed being set to 10 but then for some reason all the while-loops are continuing. It seems to be just completely ignoring the n == 0 condition - what am I doing wrong?

<!-- language: lang-py -->

from gpiozero import Button
from threading import Thread
import sys
btn1 = Button(2)
btn2 = Button(3)
cmdbtn = Button(4)
currentMorse = &quot;&quot;

def pressed1():
print(\&quot;Pressed1\&quot;)
global currentMorse
currentMorse = currentMorse + \&quot;.\&quot;
print(currentMorse)


def released1():
print(\&quot;Released\&quot;)


def pressed2():
print(\&quot;Pressed2\&quot;)
global currentMorse
currentMorse = currentMorse + \&quot;,\&quot;
print(currentMorse)


def released2():
print(\&quot;Released\&quot;)


def pressedcmd():
print(\&quot;Pressedcmd\&quot;)


def releasedcmd():
print(\&quot;Releasedcmd\&quot;)
global n
n = 10


def checker1():
global n
while n == 0:
btn1.when_pressed = pressed1
btn1.when_released = released1
print(n)


def checker2():
global n
while n == 0:
btn2.when_pressed = pressed2
btn2.when_released = released2
print(n)


def checkercmd():
global n
while n == 0:

cmdbtn.when_pressed = pressedcmd
cmdbtn.when_released = releasedcmd
print(n)

`if `name` == &#39;`main`&#39;:
n = 0
Thread(target = checker1).start()
Thread(target = checker2).start()
Thread(target = checkercmd).start()
`

I've tried putting system exits in to try and break the threads and have played around with the n condition in various ways, but nothing is changing, it just keeps accepting inputs from the buttons as if the n == 0 condition wasnt there.

答案1 {#1}

得分: 0

以下是翻译好的部分:

"when_pressed" 实际上会创建一个线程,所以我之前创建的所有其他线程都是不必要的,而且让解决问题变得更加困难。

要停止 "when_pressed",只需将其设置为 "None":

from gpiozero import Button
from threading import Thread

def start():
    global btn1
    btn1 = Button(2)
    global btn2
    btn2 = Button(3)
    global cmdbtn
    cmdbtn = Button(4)
    global currentMorse
    currentMorse = ""

    checker1()
    checker2()
    checkercmd()

def continuer():
    checker1()
    checker2()
    checkercmd()

def pressed1():
    print("Pressed1")
    global currentMorse
    currentMorse = currentMorse + "."
    print(currentMorse)

def released1():
    print("Released")

def pressed2():
    print("Pressed2")
    global currentMorse
    currentMorse = currentMorse + ","
    print(currentMorse)

def released2():
    print("Released")

def pressedcmd():
    print("Pressedcmd")

def releasedcmd():
    print("Releasedcmd")
    global n
    n = 10
    btn1.when_pressed = None
    btn1.when_released = None
    btn2.when_pressed = None
    btn2.when_released = None
    cmdbtn.when_pressed = None
    cmdbtn.when_released = None

def checker1():
    btn1.when_pressed = pressed1
    btn1.when_released = released1
    return()

def checker2():
    btn2.when_pressed = pressed2
    btn2.when_released = released2
    return()

def checkercmd():
    cmdbtn.when_pressed = pressedcmd
    cmdbtn.when_released = releasedcmd

英文:

So it turns out that when_pressed makes a thread itself - so all the other threads I made were unnecessary and made everything much harder to solve.

To stop when_pressed, you just need to set it to None:

    from gpiozero import Button
    from threading import Thread
    def start():
        global btn1
        btn1 = Button(2)
        global btn2
        btn2 = Button(3)
        global cmdbtn
        cmdbtn = Button(4)
        global currentMorse
        currentMorse = &quot;&quot;
        
        checker1()
        checker2()
        checkercmd()

    def continuer():
        checker1()
        checker2()
        checkercmd()
    
    
    def pressed1():
        print(&quot;Pressed1&quot;)
        global currentMorse
        currentMorse = currentMorse + &quot;.&quot;
        print(currentMorse)


    def released1():
        print(&quot;Released&quot;)

    def pressed2():
        print(&quot;Pressed2&quot;)
        global currentMorse
        currentMorse = currentMorse + &quot;,&quot;
        print(currentMorse)

    def released2():
        print(&quot;Released&quot;)

    def pressedcmd():
        print(&quot;Pressedcmd&quot;)


    def releasedcmd():
        print(&quot;Releasedcmd&quot;)
        global n
        n = 10
        btn1.when_pressed = None    
        btn1.when_released = None
        btn2.when_pressed = None    
        btn2.when_released = None 
        cmdbtn.when_pressed = None    
        cmdbtn.when_released = None
    
    def checker1():
        
        btn1.when_pressed = pressed1


    
        btn1.when_released = released1

        return()

    def checker2():
        
        btn2.when_pressed = pressed2



        btn2.when_released = released2
        return()


    def checkercmd():
        
        cmdbtn.when_pressed = pressedcmd



        cmdbtn.when_released = releasedcmd

赞(2)
未经允许不得转载:工具盒子 » 为什么这些功能仍然处于活动状态?Python 3