我遇到一个问题,我的python程序在从命令提示符运行时正常工作,但在导出到exe时无法正常工作。具体来说,这部分代码存在问题,也许有更好的方法可以做到这一点?:
def select_pcb_num(self, boardDrawingNumber):
xTuple = XTuple()
temp = xTuple.find_pcb_item_number(boardDrawingNumber)
if len(temp)>1:
iSelect = int(0)
rawChar = ''
query = '{0} variants found, select correct board [up/down]: {1}\t\t\t\t'
sys.stdout.write(query.format(len(temp), temp[iSelect]))
rawChar = msvcrt.getch()
while not rawChar == '\r':
if ord(rawChar) == int(72): # upkey
iSelect = (iSelect + 1)%len(temp)
elif ord(rawChar) == int(80): # downkey
iSelect = (iSelect - 1)%len(temp)
sys.stdout.write('\r')
sys.stdout.write(query.format(len(temp), temp[iSelect]))
rawChar = msvcrt.getch()
sys.stdout.write('\n')
return temp[iSelect]
else:
return temp
在命令提示符下,它会正确返回到行的开头,并在按下向上或向下箭头时将其写入。但是,当导出到exe时,它会导致重新打印相同的行,并打印正确的行。请看示例图片,不应打印带有红色箭头的线条,因为我没有选择'\ n',所以不应该有任何新行。
更新: 使用方法repr()打印的输入看起来像按下向下箭头时首先注册为'\ xe0'而不是'P',为什么编译成exe会导致这个?此外,我不明白为什么它会添加一个新行,因为它应该在while循环中
答案 0 :(得分:1)
这是Windows上getch
的记录行为。箭头键首先返回0x00或0xE0,然后是键码。见documentation:
当读取功能键或箭头键时,每个功能必须是 叫了两次;第一个调用返回0或0xE0,第二个调用返回 返回实际的密钥代码。