Chinaunix
标题:
python attribute error 还请高手指点
[打印本页]
作者:
xu_liuzhen
时间:
2013-04-25 15:50
标题:
python attribute error 还请高手指点
本帖最后由 xu_liuzhen 于 2013-04-25 15:53 编辑
用这个链接的方法获取键盘输入(
方法程序在这里
),
import select
import sys
import time
import os
import termios
def kbhit():
fd = sys.stdin.fileno()
r = select.select([sys.stdin],[],[],0.01)
rcode = ''
if len(r[0]) >0:
rcode = sys.stdin.read(1)
return rcode
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
new_settings = old_settings
#new_settings[3] = new_settings[3] & ~termios.ISIG
new_settings[3] = new_settings[3] & ~termios.ICANON
new_settings[3] = new_settings[3] & ~termios.ECHONL
print 'old setting %s'%(repr(old_settings))
termios.tcsetattr(fd,termios.TCSAFLUSH,new_settings)
while True:
c = kbhit()
if len(c) !=0 :
print 'K %s'%(c)
else:
print 'Sleep 1'
time.sleep(1)
复制代码
自己写小程序测试没有问题,添加到其他程序里就报错:
r = select.select([sys.stdin],[],[],0.01)
AttributeError: 'function' object has no attribute 'select'
还请大神们指点指点
作者:
jeppeter
时间:
2013-04-25 16:04
回复
1#
xu_liuzhen
你没有给出具体的环境是怎么样的。你是如何调用的,如果没有这些信息,是不好决定是怎么回事的。要有上下文。
作者:
xu_liuzhen
时间:
2013-04-25 16:10
补充下,我的测试小程序是这样的:
#!/usr/bin/python
import select
import sys
import time
import os
import termios
def kbhit():
fd = sys.stdin.fileno()
r = select.select([sys.stdin],[],[],0.01)
rcode = ''
print r
if len(r[0]) >0:
rcode = sys.stdin.read(1)
return rcode
def prints():
while True:
c = kbhit()
if c == 'b':
printsb()
elif c == 'c':
printsc()
elif c == 'd':
prints()
else:
print "sleep1"
time.sleep(1)
def printsb():
while True:
c = kbhit()
if c == 'b':
printsb()
elif c == 'c':
printsc()
elif c == 'd':
prints()
else:
print "b"
time.sleep(1)
def printsc():
while True:
c = kbhit()
if c == 'b':
printsb()
elif c == 'd':
prints()
else:
print "c"
time.sleep(1)
if __name__=='__main__':
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
new_settings = old_settings
new_settings[3] = new_settings[3] & ~termios.ISIG
new_settings[3] = new_settings[3] & ~termios.ICANON
new_settings[3] = new_settings[3] & ~termios.ECHONL
termios.tcsetattr(fd,termios.TCSAFLUSH,new_settings)
while True:
prints()
复制代码
我把他添加到另一个程序里面,只是替换了那些调用的输出函数,然后就报错了
作者:
xu_liuzhen
时间:
2013-04-25 16:10
不好意思我没说清楚,看我的补充
回复
2#
jeppeter
作者:
jeppeter
时间:
2013-04-25 21:19
回复
4#
xu_liuzhen
如果是你的程序,我测试过,就是没有问题,可能的原因,是这些代码没有考虑到你的调用情况。如果没有公布,可能是没有办法得到结果的。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2