简体   繁体   English

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

[英]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循环中

使用repr()打印输入

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.

相关问题 通过cmd打开python.exe时已停止工作 - python.exe has stopped working when opened via cmd cmd运行exe无法在Python中运行 - cmd to run exe not working from Python Python 程序与 cmd 一起工作正常,当我 make.exe 它不工作 - Python program works fine with cmd, when I make .exe it doens't work 我的程序在Python shell中运行,但在cmd.exe中运行时无法识别我的输入 - My program works in Python shell but doesn't recognize my input when it runs in cmd.exe Python 程序可在终端中运行,但在使用 PyInstaller 生成 EXE 时无法运行 - Python Program Working In Terminal But Not When Made Into EXE Using PyInstaller 访问.exe导出函数时,Python ctypes访问冲突 - Python ctypes access violation when accessing an .exe exported function Python - Stray cmd.exe窗口导致程序挂起。 我如何关闭这个迷路程序? - Python - Stray cmd.exe window causes program to hang. How can I close this stray program? 由外部程序运行时python exe错误 - python exe errors when running by an external program 从命令行运行程序,提示输入密码并自动为其提供密码(cmd.exe,python) - Run program from command line what prompts password and automatically provide password for it (cmd.exe, python) 从python执行程序,以便在单独的cmd.exe窗口中打开 - Executing a program from python so that it opens in separate cmd.exe window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM