Python'da terminale giriş eklerken simgeyi değiştirme

Aug 27 2020

Python'da terminalde bir oturum açma paneli oluşturuyorum ve simgeyi girişten önce dinamik olarak değiştirmek istiyorum, yani kullanıcı girişi her doldurduğunda simge değişir.

Misal:

F:\command_line>python main.py
?  username: # initially there is a question mark.

✓ username: # If the user fills the username the icon changes to ✓

Denedim:

default = '?'
onChange = '✓'
inp = input(default + " " + name + ":")
# I can't figure out how I can change it

Bunu yapmak mümkün mü? Varsa bunu nasıl başarabilirim?

Yanıtlar

Axe319 Aug 27 2020 at 02:10

Size her şeyi anlatmayacağım ama @PranavHosangadi'nin sağladığı bağlantı iyi bir kaynak.

Görünüşe göre Windows kullanıyormuşsunuz, bu nedenle cursesmodül sizin için hemen kullanılamaz. Ancak pip install windows-curses, işlevselliğin çoğunu elde edebilirsiniz . Bazı sabitlerin curses.KEY_BACKSPACEWindows sürümünde farklı olduğunu fark etsem de, bununla oynayabilir ve sizin için neyin işe yaradığını belirleyebilirsiniz.

# The module you'll use
import curses

# this is the char value that backspace returns on windows
BACKSPACE = 8

# our main function
def main(stdscr):
    # this is just to initialize the input position
    inp = '? Username:'
    stdscr.addstr(1, 1, inp)
    
    # The input from the user will be assigned to 
    # this variable
    current = ''
    # our main method of obtaining user input
    # it essentially returns control after the 
    # user inputs a character
    # the return value is essentially what ord(char) returns
    # to get the actual character you can use chr(char)
    k = stdscr.getch()

    # break the loop when the x key is pressed
    while chr(k) != 'x':
        # remove characters for backspace presses
        if k == BACKSPACE:
            if len(current):
                current = current[:len(current) - 1]
        # only allow a max of 8 characters
        elif len(current) < 8:
            current = current + chr(k)
        
        # when 8 characters are entered, change the sign
        if len(current) == 8:
            inp = '! Username:'
        else:
            inp = '? Username:'
        
        # not clearing the screen leaves old characters in place
        stdscr.clear()
        # this enters the input on row 1, column 1
        stdscr.addstr(1, 1, inp + current)
        
        # get the next user input character
        k = stdscr.getch()

if __name__ == '__main__':
    # our function needs to be driven by curses.wrapper
    curses.wrapper(main)

Bazı kaynaklar:

Resmi Belgeler

Bazı yararlı örnekler Bu, yalnızca Linux için olan bazı parçalara sahiptir, ancak bazı deneme yanılmalarla, bu kısımlar açıktır.