python 设置按键停止脚本

 时间:2024-10-27 06:56:05

1、首先,按回车键退出。#coding=utf-8raw_input(unicode('按回车键退出...','utf-8').encode('gbk'))

python 设置按键停止脚本

2、按逻晶诚湮任意键继续。import osos.system('pause')接下来即Linux下实现python版本的按任意键退出。初学Python时在总想实现一个按任意键继续/退出的程序(受.bat毒害), 奈何一直写不出来, 最近学习Unix C时发现可以通过 termios.h 库来实现, 尝试一下发现Python也有这个库, 所以终于写出一个这样的程序. 下面是代码:#!/usr/bin/env python# -*- coding:utf-8 -*-import osimport sysimport termiosdef press_any_key_exit(msg):

python 设置按键停止脚本

3、# 获取标准输入的描述符fd = sys.stdin.fileno()# 获取标准输入(终端)的设置old_ttyinfo = termios.tcgetattr烫喇霰嘴(fd)# 配置终端new_ttyinfo = old_ttyinfo[:]# 使用非规范模式(索引3是c_lflag 也就是本地模式)new_ttyinfo[3] &= ~termios.ICANON# 关闭回显(输入不会被显示)new_ttyinfo[3] &= ~termios.ECHO

python 设置按键停止脚本

4、# 输出信息sys.stdout.write(msg)sys.stdout.flush()# 使设置生效termios.tcsetattr(fd, termios.TCSANOW, new_ttyinfo)# 从终端读取os.read(fd, 7)

python 设置按键停止脚本

5、# 还原终端设置termios.tcsetattr(fd, termios.TCSANOW, old_ttyinfo)if __name__ == "__main__":press_any_key_exit("按任意键继续...")press_any_key_exit("按任意键退出...")

python 设置按键停止脚本
  • win10磁盘配额怎么设置 如何为用户分配磁盘空间
  • 解决WMIC中普通用户远程管理时拒绝访问错误
  • 虚拟机怎么修改硬盘大小
  • Win10设置账户密码方法
  • 用DOS怎么查看用户账号
  • 热门搜索
    英语手抄报简单又漂亮 手抄报 劳动手抄报 四年级手抄报 清明节手抄报图片 经典诵读手抄报 感恩父母手抄报 手抄报版面 关于节约用水的手抄报 爱国手抄报