[英]Python program working in CMD but not when exported to .exe
I'm having an issue where my python program works correctly when ran from the command prompt but does not function properly when exported to an exe. 我遇到一个问题,其中从命令提示符运行时我的python程序可以正常运行,但是导出到exe时不能正常运行。 Specifically I'm having an issue with this section of code, maybe there is a better way of doing this?: 具体来说,我在这段代码中遇到问题,也许有更好的方法可以做到这一点?:
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
In command prompt it correctly returns to the beginning of the line and writes over it when an up or down arrow is pressed. 在命令提示符下,它可以正确返回到行的开头,并在按下向上或向下箭头时覆盖它。 However when exported to an exe it causes the same line to be reprinted and than prints the correct line. 但是,导出到exe时,它会导致重新打印同一行,然后打印正确的行。 Please see the example picture, the lines with the red arrows should not be printed and there shouldn't have been any new lines since I didn't get to the '\\n' because no selection was made. 请查看示例图片,不应该打印带有红色箭头的行,并且不应该有任何新行,因为我没有进入'\\ n',因为没有选择。
Update: Input printed with the method repr() looks like when the down arrow is pressed it first registers as '\\xe0' and than as 'P', why would having compiled to an exe cause this? 更新:使用repr()方法打印的输入看起来像当按下向下箭头时首先注册为'\\ xe0'而不是'P',为什么编译为exe会导致这种情况? Also I don't see why it's adding a new line since it should be in the while loop 我也看不到为什么要添加新行,因为它应该在while循环中
This is the documented behaviour of getch
on Windows. 这是Windows上getch
的已记录行为。 Arrow keys return first either 0x00 or 0xE0, then the key code. 箭头键首先返回0x00或0xE0,然后返回键代码。 See documentation : 参见文档 :
When reading a function key or an arrow key, each function must be called twice; 读取功能键或箭头键时,每个功能必须调用两次; the first call returns 0 or 0xE0, and the second call returns the actual key code. 第一次调用返回0或0xE0,第二次调用返回实际的密钥代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.