Python-循环等待输入 [英] Python - Loop waiting for input

查看:465
本文介绍了Python-循环等待输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow论坛上四处张望,找不到适合我特定问题的解决方案.

I have looked around on the StackOverflow forums, unable to find a solution that applies to my specific problem.

我需要写一些代码,不断检查用户输入. 我有一个带有条形码扫描仪的Raspberry Pi.我希望我的Python脚本循环播放,等待我的条形码扫描仪发出声音(它会在活动窗口中键入"它,就像键盘一样).当条形码扫描仪键入" 8位数字时-我需要Python脚本停止-接受输入并将其保存在变量中.

I need to write a bit of code, that is continuously checking for user input. I have got a Raspberry Pi with a Barcode Scanner attached to it. I want my Python script to loop, waiting for my Barcode Scanner to bleep something (which will then "type" it in the active window, it's acting like a keyboard). When the barcode scanner 'types' the 8 digit number - I need the Python script to stop - take the input and save it in a variable.

这是我唯一能提出的伪代码:

This is the only psuedocode I could come up with:

// Create variable, store an empty string

// Create a while loop
// Within the while loop, continuously check for input. 
// If input has been found, stop the loop and save the input in a variable.

非常抱歉,我无法提供自己的代码-我只是不知道从哪里开始.

I am terribly sorry I couldn't come up with my own code - I just have no idea where to start.

扫描仪将数字键入".但是不按ENTER键.所以我不知道该如何编程.

The scanner 'types' the digits out. But does not press ENTER. So I have no idea how I can program around that.

推荐答案

您可以在RaspberryPi上使用此库: https://pypi.python.org/pypi/readchar

You can use this library on a RaspberryPi: https://pypi.python.org/pypi/readchar

import readchar


inputStr = ""

while len(inputStr) != 8:
    inputStr += str(readchar.readchar())

# Quote: "Save it in variable"

variable = inputStr

# Clean
inputStr = ""

或者,要缩短所有时间:

Or, to shorten everything:

import readchar

variable = ""
while len(variable) != 8:
    variable += str(readchar.readchar())

这篇关于Python-循环等待输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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