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

huangapple go评论110阅读模式
英文:

Why are these functions still active? Python 3

问题

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

  1. from gpiozero import Button
  2. from threading import Thread
  3. import sys
  4. btn1 = Button(2)
  5. btn2 = Button(3)
  6. cmdbtn = Button(4)
  7. currentMorse = ""
  8. def pressed1():
  9. print("Pressed1")
  10. global currentMorse
  11. currentMorse = currentMorse + "."
  12. print(currentMorse)
  13. def released1():
  14. print("Released")
  15. def pressed2():
  16. print("Pressed2")
  17. global currentMorse
  18. currentMorse = currentMorse + ","
  19. print(currentMorse)
  20. def released2():
  21. print("Released")
  22. def pressedcmd():
  23. print("Pressedcmd")
  24. def releasedcmd():
  25. print("Releasedcmd")
  26. global n
  27. n = 10
  28. def checker1():
  29. global n
  30. while n == 0:
  31. btn1.when_pressed = pressed1
  32. btn1.when_released = released1
  33. print(n)
  34. def checker2():
  35. global n
  36. while n == 0:
  37. btn2.when_pressed = pressed2
  38. btn2.when_released = released2
  39. print(n)
  40. def checkercmd():
  41. global n
  42. while n == 0:
  43. cmdbtn.when_pressed = pressedcmd
  44. cmdbtn.when_released = releasedcmd
  45. print(n)
  46. if __name__ == '__main__':
  47. n = 0
  48. Thread(target=checker1).start()
  49. Thread(target=checker2).start()
  50. 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 -->

  1. from gpiozero import Button
  2. from threading import Thread
  3. import sys
  4. btn1 = Button(2)
  5. btn2 = Button(3)
  6. cmdbtn = Button(4)
  7. currentMorse = &quot;&quot;
  8. def pressed1():
  9. print(&quot;Pressed1&quot;)
  10. global currentMorse
  11. currentMorse = currentMorse + &quot;.&quot;
  12. print(currentMorse)
  13. def released1():
  14. print(&quot;Released&quot;)
  15. def pressed2():
  16. print(&quot;Pressed2&quot;)
  17. global currentMorse
  18. currentMorse = currentMorse + &quot;,&quot;
  19. print(currentMorse)
  20. def released2():
  21. print(&quot;Released&quot;)
  22. def pressedcmd():
  23. print(&quot;Pressedcmd&quot;)
  24. def releasedcmd():
  25. print(&quot;Releasedcmd&quot;)
  26. global n
  27. n = 10
  28. def checker1():
  29. global n
  30. while n == 0:
  31. btn1.when_pressed = pressed1
  32. btn1.when_released = released1
  33. print(n)
  34. def checker2():
  35. global n
  36. while n == 0:
  37. btn2.when_pressed = pressed2
  38. btn2.when_released = released2
  39. print(n)
  40. def checkercmd():
  41. global n
  42. while n == 0:
  43. cmdbtn.when_pressed = pressedcmd
  44. cmdbtn.when_released = releasedcmd
  45. print(n)
  46. if __name__ == &#39;__main__&#39;:
  47. n = 0
  48. Thread(target = checker1).start()
  49. Thread(target = checker2).start()
  50. 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

得分: 0

以下是翻译好的部分:

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

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

  1. from gpiozero import Button
  2. from threading import Thread
  3. def start():
  4. global btn1
  5. btn1 = Button(2)
  6. global btn2
  7. btn2 = Button(3)
  8. global cmdbtn
  9. cmdbtn = Button(4)
  10. global currentMorse
  11. currentMorse = ""
  12. checker1()
  13. checker2()
  14. checkercmd()
  15. def continuer():
  16. checker1()
  17. checker2()
  18. checkercmd()
  19. def pressed1():
  20. print("Pressed1")
  21. global currentMorse
  22. currentMorse = currentMorse + "."
  23. print(currentMorse)
  24. def released1():
  25. print("Released")
  26. def pressed2():
  27. print("Pressed2")
  28. global currentMorse
  29. currentMorse = currentMorse + ","
  30. print(currentMorse)
  31. def released2():
  32. print("Released")
  33. def pressedcmd():
  34. print("Pressedcmd")
  35. def releasedcmd():
  36. print("Releasedcmd")
  37. global n
  38. n = 10
  39. btn1.when_pressed = None
  40. btn1.when_released = None
  41. btn2.when_pressed = None
  42. btn2.when_released = None
  43. cmdbtn.when_pressed = None
  44. cmdbtn.when_released = None
  45. def checker1():
  46. btn1.when_pressed = pressed1
  47. btn1.when_released = released1
  48. return()
  49. def checker2():
  50. btn2.when_pressed = pressed2
  51. btn2.when_released = released2
  52. return()
  53. def checkercmd():
  54. cmdbtn.when_pressed = pressedcmd
  55. 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:

  1. from gpiozero import Button
  2. from threading import Thread
  3. def start():
  4. global btn1
  5. btn1 = Button(2)
  6. global btn2
  7. btn2 = Button(3)
  8. global cmdbtn
  9. cmdbtn = Button(4)
  10. global currentMorse
  11. currentMorse = &quot;&quot;
  12. checker1()
  13. checker2()
  14. checkercmd()
  15. def continuer():
  16. checker1()
  17. checker2()
  18. checkercmd()
  19. def pressed1():
  20. print(&quot;Pressed1&quot;)
  21. global currentMorse
  22. currentMorse = currentMorse + &quot;.&quot;
  23. print(currentMorse)
  24. def released1():
  25. print(&quot;Released&quot;)
  26. def pressed2():
  27. print(&quot;Pressed2&quot;)
  28. global currentMorse
  29. currentMorse = currentMorse + &quot;,&quot;
  30. print(currentMorse)
  31. def released2():
  32. print(&quot;Released&quot;)
  33. def pressedcmd():
  34. print(&quot;Pressedcmd&quot;)
  35. def releasedcmd():
  36. print(&quot;Releasedcmd&quot;)
  37. global n
  38. n = 10
  39. btn1.when_pressed = None
  40. btn1.when_released = None
  41. btn2.when_pressed = None
  42. btn2.when_released = None
  43. cmdbtn.when_pressed = None
  44. cmdbtn.when_released = None
  45. def checker1():
  46. btn1.when_pressed = pressed1
  47. btn1.when_released = released1
  48. return()
  49. def checker2():
  50. btn2.when_pressed = pressed2
  51. btn2.when_released = released2
  52. return()
  53. def checkercmd():
  54. cmdbtn.when_pressed = pressedcmd
  55. cmdbtn.when_released = releasedcmd

huangapple
  • 本文由 发表于 2023年8月11日 00:03:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定