V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zictos
V2EX  ›  Python

如何防止选中命令行工具上的文字时导致 print()阻塞?

  •  
  •   zictos · 2022-03-21 20:15:47 +08:00 · 2141 次点击
    这是一个创建于 781 天前的主题,其中的信息可能已经有所发展或是发生改变。

    windows 上, 选中命令行工具上的文字就会导致 print()阻塞。

    解决办法:
    1 、在 cmd 的设置中关闭的“快速编辑模式”,但是这样想复制文字的时候就无法复制了。
    2 、使用其他命令行工具,但不清楚哪个好用点。不过使用这个方案后 pyinstaller 生成的 exe 文件也不能直接双击运行,要先打开其他命令行工具再运行 exe 文件,否则还是会有一样的问题。

    不知道有没有更完美的方案,另外 cmd 是否有相关“参数”可以让自己在启动时单独关闭自身窗口的“快速编辑模式”,而不是关闭所有 cmd 窗口的“快速编辑模式”

    5 条回复    2022-03-21 22:11:06 +08:00
    JeffGe
        1
    JeffGe  
       2022-03-21 20:51:54 +08:00 via Android   ❤️ 1
    Windows Terminal
    ec0
        2
    ec0  
       2022-03-21 21:41:22 +08:00   ❤️ 1
    import ctypes

    kernel32 = ctypes.windll.kernel32
    kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), 128)

    代码来自
    https://stackoverflow.com/questions/37500076/how-to-enable-windows-console-quickedit-mode-from-python
    ec0
        3
    ec0  
       2022-03-21 21:48:54 +08:00
    但是上述代码好像会使 input 函数无法返回
    ec0
        4
    ec0  
       2022-03-21 22:04:08 +08:00   ❤️ 1
    把 128 改成 439 可以使 input 函数正常返回
    zictos
        5
    zictos  
    OP
       2022-03-21 22:11:06 +08:00
    @ec0 #4 嗯,439 好一点,不会禁用“插入模式”。
    这样的方案就差不多可以了,只会禁用当前窗口的快速编辑模式,不会影响其他窗口。如果临时需要复制上面的文字就再手动开启快速编辑模式即可。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1235 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 18:13 · PVG 02:13 · LAX 11:13 · JFK 14:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.