Windows环境下如何在同一控制台实现无循环中断的ASCII动画与登录界面
Solution for Windows Console: ASCII Animation + Login UI (No Curses Needed)
Hey there! I totally get the frustration with curses on Windows—it's never been a smooth fit. Let's build a solution using Python that plays nicely with Windows consoles, using multi-threading to run the ASCII animation and login UI side by side.
Key Tools We'll Use
colorama: Lets us use ANSI escape codes for cursor positioning and text formatting (Windows doesn't support these natively by default)threading: Separates the animation loop from the login input handling so they run concurrentlymsvcrt: Handles raw keyboard input (so we can capture password keystrokes without waiting for Enter, and hide the password with *)
Step-by-Step Implementation
First, install the required package:
pip install colorama
Full Code Example
import threading import time import msvcrt import colorama from colorama import Fore, Back # Initialize colorama to enable ANSI codes on Windows colorama.init(autoreset=True) # ---------------------- # ASCII Animation Frames # ---------------------- ANSI_FRAMES = [ r""" o-o / \ / \ | | \ / \ / o-o """, r""" o-o / \ / \ | | \ / \ / / o-o """, r""" o-o / \ / \ | | \ / / \ / / o-o """, r""" o-o / \ / \ | | \ / / / \ / / o-o """, r""" o-o / \ / \ | | / \ / / / / / o-o """, ] # Global flag to control animation thread animation_running = True def animate_ascii(): """Thread function to loop through ASCII animation frames""" frame_index = 0 while animation_running: # Move cursor to top-left corner (row 1, column 1) print("\033[1;1H", end="") # Clear the animation area (first 8 rows) for _ in range(8): print("\033[K", end="\n") # Move cursor back to top-left print("\033[1;1H", end="") # Print current frame with color print(Fore.CYAN + ANSI_FRAMES[frame_index]) # Cycle frames frame_index = (frame_index + 1) % len(ANSI_FRAMES) # Frame delay time.sleep(0.2) def show_login_ui(): """Function to display login form and handle input""" # Move cursor to row 10, column 1 (below animation area) print("\033[10;1H", end="") print(Fore.GREEN + "=== Login ===") print("\033[11;1H", end="") username = input(Fore.WHITE + "Username: ") # Handle password input (hide characters with *) print("\033[12;1H", end="") print(Fore.WHITE + "Password: ", end="") password = "" while True: key = msvcrt.getch() # Exit on Enter key if key == b'\r': break # Handle backspace elif key == b'\x08': if password: password = password[:-1] # Erase last character from console print("\b \b", end="", flush=True) # Handle printable characters else: try: char = key.decode('utf-8') password += char print("*", end="", flush=True) except: pass print("\n") print(Fore.YELLOW + f"Login attempt for user: {username}") # Stop animation after login global animation_running animation_running = False if __name__ == "__main__": try: # Hide cursor for cleaner appearance print("\033[?25l", end="") # Start animation thread anim_thread = threading.Thread(target=animate_ascii) anim_thread.daemon = True anim_thread.start() # Run login UI in main thread show_login_ui() finally: # Restore cursor visibility and reset console print("\033[?25h", end="") colorama.deinit()
How This Works
- Animation Thread: Runs in the background, constantly updating the top portion of the console with new animation frames. We use ANSI escape codes (
\033[1;1H) to jump the cursor to the top-left corner, clear the previous frame, and print the new one. - Login UI: Lives in the lower portion of the console (starting at row 10). For the password field, we use
msvcrt.getch()to capture each keystroke instantly, replacing characters with*to keep the password hidden. - Console Cleanup: We hide the cursor during execution and restore it when done, plus use
colorama.deinit()to reset the console to its default state.
Customization Tips
- Adjust the animation frame delay (
time.sleep(0.2)) to make the animation faster or slower. - Modify the
ANSI_FRAMESlist to use your own ASCII art. - Change the row numbers in
\033[n;1Hto reposition the animation or login UI. - Add more styling with
colorama'sFore/Backclasses for colored text backgrounds.
内容的提问来源于stack exchange,提问作者Wowotek
相关产品推荐
相关产品推荐

