简体   繁体   中英

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. 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. 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.

在此处输入图片说明

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? Also I don't see why it's adding a new line since it should be in the while loop

使用repr()打印输入

This is the documented behaviour of getch on Windows. Arrow keys return first either 0x00 or 0xE0, then the key code. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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