免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1591 | 回复: 4

python attribute error 还请高手指点 [复制链接]

论坛徽章:
0
发表于 2013-04-25 15:50 |显示全部楼层
本帖最后由 xu_liuzhen 于 2013-04-25 15:53 编辑

用这个链接的方法获取键盘输入(方法程序在这里),
  1. import select
  2. import sys
  3. import time
  4. import os
  5. import termios



  6. def kbhit():
  7.     fd = sys.stdin.fileno()
  8.     r = select.select([sys.stdin],[],[],0.01)
  9.     rcode = ''
  10.     if len(r[0]) >0:
  11.         rcode  = sys.stdin.read(1)
  12.     return rcode


  13. fd = sys.stdin.fileno()
  14. old_settings = termios.tcgetattr(fd)
  15. new_settings = old_settings
  16. #new_settings[3] = new_settings[3] & ~termios.ISIG
  17. new_settings[3] = new_settings[3] & ~termios.ICANON
  18. new_settings[3] = new_settings[3] & ~termios.ECHONL
  19. print 'old setting %s'%(repr(old_settings))
  20. termios.tcsetattr(fd,termios.TCSAFLUSH,new_settings)

  21. while True:
  22.     c = kbhit()
  23.     if len(c) !=0 :
  24.         print 'K %s'%(c)
  25.     else:
  26.         print 'Sleep 1'
  27.         time.sleep(1)
复制代码
自己写小程序测试没有问题,添加到其他程序里就报错:

    r = select.select([sys.stdin],[],[],0.01)
AttributeError: 'function' object has no attribute 'select'

还请大神们指点指点

论坛徽章:
1
15-16赛季CBA联赛之新疆
日期:2017-03-09 12:33:45
发表于 2013-04-25 16:04 |显示全部楼层
回复 1# xu_liuzhen


    你没有给出具体的环境是怎么样的。你是如何调用的,如果没有这些信息,是不好决定是怎么回事的。要有上下文。

论坛徽章:
0
发表于 2013-04-25 16:10 |显示全部楼层
补充下,我的测试小程序是这样的:
  1. #!/usr/bin/python
  2. import select
  3. import sys
  4. import time
  5. import os
  6. import termios



  7. def kbhit():
  8.     fd = sys.stdin.fileno()
  9.     r = select.select([sys.stdin],[],[],0.01)
  10.     rcode = ''
  11.     print r
  12.     if len(r[0]) >0:
  13.         rcode  = sys.stdin.read(1)
  14.     return rcode


  15. def prints():
  16.         while True:
  17.                 c = kbhit()
  18.                 if c == 'b':
  19.                         printsb()
  20.                 elif c == 'c':
  21.                         printsc()
  22.                 elif c == 'd':
  23.                         prints()
  24.                 else:
  25.                         print "sleep1"
  26.                         time.sleep(1)

  27. def printsb():
  28.         while True:
  29.                 c = kbhit()
  30.                 if c == 'b':
  31.                         printsb()
  32.                 elif c == 'c':
  33.                         printsc()
  34.                 elif c == 'd':
  35.                         prints()
  36.                 else:
  37.                         print "b"
  38.                         time.sleep(1)



  39. def printsc():
  40.     while True:
  41.         c = kbhit()
  42.         if c == 'b':
  43.             printsb()
  44.         elif c == 'd':
  45.             prints()
  46.         else:
  47.             print "c"
  48.             time.sleep(1)

  49. if __name__=='__main__':
  50.     fd = sys.stdin.fileno()
  51.     old_settings = termios.tcgetattr(fd)
  52.     new_settings = old_settings
  53.     new_settings[3] = new_settings[3] & ~termios.ISIG
  54.     new_settings[3] = new_settings[3] & ~termios.ICANON
  55.     new_settings[3] = new_settings[3] & ~termios.ECHONL
  56.     termios.tcsetattr(fd,termios.TCSAFLUSH,new_settings)


  57.     while True:
  58.             prints()
复制代码
我把他添加到另一个程序里面,只是替换了那些调用的输出函数,然后就报错了

论坛徽章:
0
发表于 2013-04-25 16:10 |显示全部楼层
不好意思我没说清楚,看我的补充回复 2# jeppeter


   

论坛徽章:
1
15-16赛季CBA联赛之新疆
日期:2017-03-09 12:33:45
发表于 2013-04-25 21:19 |显示全部楼层
回复 4# xu_liuzhen


    如果是你的程序,我测试过,就是没有问题,可能的原因,是这些代码没有考虑到你的调用情况。如果没有公布,可能是没有办法得到结果的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP