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

查看:35
本文介绍了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.

推荐答案

你可以在树莓派上使用这个库: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 = ""

或者,缩短一切:

import readchar

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

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

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