原代码来自

class _Getch:

"""Gets a single character from standard input. Does not echo to the

screen."""

def __init__(self):

try:

self.impl = _GetchWindows()

except ImportError:

self.impl = _GetchUnix()

def __call__(self): return self.impl()

class _GetchUnix:

def __init__(self):

import tty, sys

def __call__(self):

import sys, tty, termios

fd = sys.stdin.fileno()

old_settings = termios.tcgetattr(fd)

try:

tty.setraw(sys.stdin.fileno())

ch = sys.stdin.read(1)

finally:

termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

return ch

class _GetchWindows:

def __init__(self):

import msvcrt

def __call__(self):

import msvcrt

return msvcrt.getch()

getch = _Getch()

使用的时候可以这么用,比如输入时不仅可以将输入的字符显示在屏幕上,就像是input(), 同时还可以对其每个字符都在输入时进行操作, 最后按下回车键退出输入

import sys

ss =''

while True:

s = Getch()

if s == "\r": # 注意回车键是 \r, 不是 \n

break

print(s,end='')

sys.stdout.flush() # 刷新缓冲区,不然无法显示到屏幕上

ss += s

print()

print(ss)

还可以在类中加上

def __str__(self):

return self.impl # 返回是其实是个字符串

然后使用:

str = str(_Getch()) # 自动调用__str__(self)方法

然后是我的精简魔改版

def Getch():

def _GetchUnix():

import sys, tty, termios

fd = sys.stdin.fileno()

old_settings = termios.tcgetattr(fd)

try:

tty.setraw(sys.stdin.fileno())

ch = sys.stdin.read(1)

finally:

termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

return ch

def _GetchWindows():

import msvcrt

return msvcrt.getch()

try:

impl = _GetchWindows()

except ImportError:

impl = _GetchUnix()

return impl

使用:

str = Getch()

即可

魔改精简版2

上一个代码如果把Getch()放到循环里会有个问题,就是上下左右键可以控制光标的移动,这有些惊奇,但也是我不想要的

class Getch():

def __init__(self):

try:

self.impl = self._GetchWindows()

except ImportError:

self.impl = self._GetchUnix()

def __str__(self):

return self.impl

def _GetchUnix(self):

import sys, tty, termios

fd = sys.stdin.fileno()

old_settings = termios.tcgetattr(fd)

try:

tty.setraw(sys.stdin.fileno())

ch = sys.stdin.read(1)

finally:

termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

return ch

def _GetchWindows(self):

import msvcrt

return msvcrt.getch()

使用

import sys

ss =''

while True:

s = Getch()

s = str(Getch())

if s == "\r":

break

print(s,end='')

sys.stdout.flush()

ss += s

方法3.使用getch库

这是一个仿照windows下conio.h中的getch()函数的库

https://pypi.org/project/getch/

产生这个问题的原因是使用tcp连接通信,在终端输入一句话时,如果只输入了一半,突然接收到了对方发来的消息,就会从光标处打印在屏幕上,那么我输入的话就被打断了.

查了很多网页,不知道getch这么个东西,学C语言时压根不知道,太差了.

python getch函数_pyhton 下 使用getch(), 输入字符无需回车相关推荐

  1. 解决SublimeREPL安装后输入字符且回车后没有输出的问题

    解决SublimeREPL安装后输入字符且回车后没有输出的问题 提供一个可能的思路:有可能需要使用 多个空格 或者 空格+;分号 作为输入结束符号. 我的SublimeREPL确实有时候会遇到回车输入 ...

  2. python 编写函数,实现根据键盘输入的长、宽、高之值计算长方体体积

    编写函数,实现根据键盘输入的长.宽.高之值计算长方体体积 n1 = input("长:") n2 = input("宽:") m =input("高: ...

  3. python global用法_14_手把手教你学Python之函数(下)

    变量作用域:根据变量定义的位置,可将变量分为全局变量和局部变量. 全局变量:定义在函数外面的变量,可以在多个函数中进行访问,但不能执行赋值操作.如果有赋值语句,相当于创建了一个同名的局部变量: 局部变 ...

  4. 如何在DOS下不显示输入字符

    @echo off echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5x>in.com set  /p password=Enter passwor ...

  5. python getch_macOS 下的 getch()

    macOS 下的 getch() 我想在c语言写的某程序实现 "按下任意键继续...".这在windows 下好实现,用getch()就行了. 但是,macOS 用同样的办法,cl ...

  6. linux中c语言kbhit函数用法,linux下kbhit()函数 getch函数。

    对于上面的问题,都用到 linux下的getch函数与kbhit函数. 参考了http://kpld8888.wordpress.com/2007/03/07/linux%E4%B8%8B%E7%9A ...

  7. C语言getch()函数学习

    getch()要点如下: 这个函数是一个不回显函数,当用户按下某个字符时,函数自动读取,无需按回车:     这个函数并非标准函数: 所在头文件:conio.h     函数用途:从控制台读取一个字符 ...

  8. 在linux中使用getch()函数

    #include <termio.h>int getch(void) {struct termios tm, tm_old;int fd = 0, ch;if (tcgetattr(fd, ...

  9. UNIX_C 环境下实现输入一个字符,不用回车直接输入功能(类型windows下_getch(void)函数)

    UNIX_C 环境下实现输入一个字符,不用回车直接输入功能(类型windows下_getch(void)函数) /*int getch ( void ); 输入流获取一个信号当键盘输入一个字符时,不用 ...

最新文章

  1. Windows Server中的故障转移群集的实现机制
  2. 潘建伟团队最新研究成果登上Nature:首次实现1120公里长距离无中继纠缠量子密钥分发...
  3. 【JavaScript】【PPT】继承的本质
  4. 有BUG!!!慎用default文本
  5. 谷歌:不守规矩的“顽童”
  6. JAVA入门级教学之(布尔型数据类型)
  7. vfork()系统调用
  8. 教你如何在@ViewChild查询之前获取ViewContainerRef
  9. 狼殿下高清壁纸|不用等的好剧!
  10. Enum定义位域, 即可以通过位操作来产生未命名的值
  11. 数学建模day1 层次分析法与TOPSIS方法
  12. 利用Pano2VR在全景图中添加视频,音频,图片
  13. 动态域名解析ipv6 群辉dnspod_群晖IPV6 DDNS设置终极大全(移动用户进)(二)
  14. UVA11584划分回文串
  15. ubuntu18与win10双系统引导修复
  16. 【5G RRC】Master Information Block (NR-MIB)
  17. 如何利用“跑腿系统”来提高生活效率?
  18. 电脑里的文档不小心删除了恢复的方法
  19. hostiko模板-WHMCS自适应模板-略站网
  20. 2021年中国大学生程序设计竞赛女生专场 gym103389F 地图压缩

热门文章

  1. note GAN model
  2. 洗脑神曲《萨瓦迪卡曼谷》MV里的旅游景点,你都打卡了吗?
  3. 全球与中国手持式凿岩机市场供需情况分析及十四五趋势预测报告2022-2027年版
  4. 初等函数的麦克劳林级数展开+逆函数的展开求法
  5. 详解OpenCV的椭圆绘制函数ellipse()
  6. CSS实现固定宽高比响应式布局(附实例分析)
  7. 节假日读取接口_2018年节假日API接口,直接计算好的
  8. (4)pokeman_用图片对模型进行测试
  9. [虚拟机]hyper-v简介及安装使用(图文详解)
  10. python随风飘落怎么画_树叶飘落动画制作 如何制作树叶飘落的动画?视频画面添加树叶随风飘落的动画效果...