计划目的:
- 蟒蛇 3
- PyCharm IDE
- 树莓派 4
在开始之前,让我先说一下我几天前才开始学习 Python 和编码,所以我知道我可能在学习过程中遗漏了一些东西。
该程序的预期功能是让交流受限的人能够说话或至少进行基本的交流。该程序所做的是开始在选项之间自动循环,其间有设定的时间间隔。这允许选项显示足够长的时间,以便人们单击单个按钮来选择该选项。然后将显示该选项。整个前提是通过单击按钮或其他单一感官输入来促进交流。这个来自更大程序的示例只是询问用户“是吗?” 或者没有?” 但其余程序功能使用相同的格式。目前,将选择“否”用于测试目的,因为在我添加 GPIO 功能之前它是硬编码的。
问题:
该程序完全符合我的需要,但现在我想让通常在 IDE 中使用 print() 返回的文本显示在 7" 屏幕上,以使其更加用户友好。因此,我需要制作一个 GUI。我需要它做的就是显示相同的文本。但是,从我一直在阅读的内容来看,由于连续显示循环,我的程序几乎没有一个适用于 GUI。
不能使用while循环
不能使用 sleep.time
这是该程序最重要的两个功能...我试图找到可以与 tkinter 或 Guizero 等基本功能一起使用的替代方案,但似乎没有什么是一个简单的解决方案。我看过线程,以及 Guizero 的 after/repeat 功能,但到目前为止还没有让它们工作。
我需要解决什么来解决问题:
- 处理自动选项循环间隔的不同方法
- 一种替换while循环的方法
这个循环问题是否有一个简单的编程修复?我可以通过使用外部时钟并通过 GPIO 引脚读取它来以某种方式克服这个问题吗?非常感谢我能得到的任何帮助。与此同时,我会继续寻找解决方案。谢谢!
import time
def yesnomode():
count = 0
switch_state = 15 # Variable that holds live value
switch_thresh = 10 # Variable to tune the threshold
button_state = 0 # Variable that holds live value
button_thresh = 10 # Variable to tune the threshold
rate = 1.5 # Rate that the options will cycle
print("Beginning Yes or No Mode")
while switch_state >= switch_thresh: # if switch reaches threshold then begin letter cycling
count = count + 1
time.sleep(rate) # This is the rate that the letters will cycle
if count == 1:
print("Yes?")
if button_state >= button_thresh:
print("Yes Selected")
time.sleep(rate)
if count == 2:
print("No?")
button_state = 10
if button_state >= button_thresh:
time.sleep(rate)
print("No Selected")
button_state = 0
if count == 3:
count = 0
yesnomode()