繁体   English   中英

Python程序可在CMD中运行,但导出到.exe时无法

[英]Python program working in CMD but not when exported to .exe

我遇到一个问题,其中从命令提示符运行时我的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循环中

使用repr()打印输入

这是Windows上getch的已记录行为。 箭头键首先返回0x00或0xE0,然后返回键代码。 参见文档

读取功能键或箭头键时,每个功能必须调用两次; 第一次调用返回0或0xE0,第二次调用返回实际的密钥代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM