python等待用户输入10秒_Python-循环等待输入

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

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.

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

解决方案

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())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值