免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5758 | 回复: 5
打印 上一主题 下一主题

请教如何设置raw_input的等待时间? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-04 07:37 |只看该作者 |倒序浏览
例如设置多少秒没有输入就自动跳过

论坛徽章:
0
2 [报告]
发表于 2012-04-04 09:40 |只看该作者
本帖最后由 anonymous0502 于 2012-04-04 09:40 编辑

这有个别人写的例子,我试了能运行:
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import time
  5. import msvcrt


  6. def readInput(caption, default, timeout=5):
  7.     start_time = time.time()
  8.     sys.stdout.write('%s(%s):' % (caption, default))
  9.     input = ''
  10.     while True:
  11.         if msvcrt.kbhit():
  12.             chr = msvcrt.getche()
  13.             if ord(chr) == 13:  # enter_key
  14.                 break
  15.             elif ord(chr) >= 32:

  16.                                  # space_char

  17.                 input += chr
  18.         if len(input) == 0 and time.time() - start_time > timeout:
  19.             break

  20.     print ''  # needed to move to next line
  21.     if len(input) > 0:
  22.         return input
  23.     else:
  24.         return default


  25. # and some examples of usage

  26. ans = readInput('Please type a name', 'john')
  27. print 'The name is %s' % ans
  28. ans = readInput('Please enter a number', 10)
  29. print 'The number is %s' % ans
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-04-04 12:09 |只看该作者
上面的代码测试成功,不过不太明白代码的含义

尝试改为easygui.enterbox("Enter Code: "),却不成功

论坛徽章:
0
4 [报告]
发表于 2012-04-04 13:08 |只看该作者
没有用过easygui,不太清楚了。

论坛徽章:
0
5 [报告]
发表于 2012-04-04 20:24 |只看该作者
多谢,继续折腾看看

论坛徽章:
0
6 [报告]
发表于 2012-04-10 06:50 |只看该作者
   if len(input) == 0 and time.time() - start_time > timeout:

            break

关键就是这一句
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP