进入Python命令行使用Backspace和方向键出现乱码

本文介绍了解决在CentOS7.2系统上使用Python 2.7.12命令行时遇到的Backspace及方向键乱码问题的方法。通过安装readline及readline-devel.x86_64并重新编译Python,使Backspace和方向键恢复正常。

进入Python命令行使用Backspace和方向键出现乱码

Linux服务器版本:CentOS 7.2

Python版本:2.7.12

问题描述:在腾讯云服务器安装好Python后,输入python进入命令行,然后输入内容后,想通过Backspace进行回退删除输入错误的内容显示^H ,以及通过方向键移动光标时显示^[[A 等乱码!

解决方案:

明确自己的系统有readline库,可以直接输入 yum -y install readline 进行验证,如果没有则会自动安装,如果已经有了,会出现下图提示:
然后再验证是否有readline的开发包,如果没有输入 yum install readline-devel.x86_64进行安装;
安装好上述之后,重现进入Python的解压文件夹中进行编译:
./configure
make
make install
最后再次进入Python命令行, Backspace和方向键就可以正常使用了,问题圆满解决!

https://my.oschina.net/aibinxiao/blog/1236076

出错的代码: import pygame import sys import os # 初始化Pygame pygame.init() # 获取屏幕分辨率并设置全屏 screen_info = pygame.display.Info() screen = pygame.display.set_mode((screen_info.current_w, screen_info.current_h), pygame.FULLSCREEN) pygame.display.set_caption("Pygame CMD Terminal") # 设置颜色 BLACK = (0, 0, 0) GREEN = (0, 255, 0) # 初始化字体 font = pygame.font.SysFont("Consolas", 24) # 使用等宽字体更接近CMD效果 # 输入缓冲区显示缓冲区 input_buffer = [] output_buffer = [] command_history = [] history_index = -1 # 主循环 running = True while running: screen.fill(BLACK) # 清屏 # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT or \ (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: # 回车执行命令 command = ''.join(input_buffer) output_buffer.append(os.system(command)) input_buffer = [] command_history.append(command) history_index = len(command_history) - 1 elif event.key == pygame.K_UP: # 上方向键调取历史命令 if command_history: history_index = max(0, history_index - 1) input_buffer = list(command_history[history_index]) elif event.key == pygame.K_DOWN: # 下方向键调取历史命令 if command_history: history_index = min(len(command_history)-1, history_index + 1) input_buffer = list(command_history[history_index]) else: input_buffer.append(event.unicode) # 渲染输出内容 y_offset = 50 for line in output_buffer[-20:]: # 显示最后20行输出 text = font.render(line, True, GREEN) screen.blit(text, (20, y_offset)) y_offset += 30 # 渲染输入提示 prompt = "> " + ''.join(input_buffer) text = font.render(prompt, True, GREEN) screen.blit(text, (20, y_offset)) pygame.display.flip() pygame.quit() sys.exit()
最新发布
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值