输入操作时?

发布于 2024-10-27 02:17:04 字数 103 浏览 7 评论 0原文

嘿,我正在开发一个 python 项目,该项目需要花费几分钟的时间。问题是,由于需要几分钟,我希望用户能够按 Enter 键来查看操作的当前状态。我怎样才能在 Python 2 中做到这一点?

Hey I'm working on a python project that requires an action that takes a couple minutes. The thing is since it takes a couple minutes I'd like the user to be able to press enter to see the current status of the action. How can I do this in Python 2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-11-03 02:17:04

@Space_C0wb0y 是对的,进度条是一个很好的解决方案。然而,这展示了一种完成您要求的方法。从这里摘取了一些代码: Non-blocking read on a subprocess.PIPE in python

import fcntl, os, sys

# make stdin a non-blocking file
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

def enter_pressed():
    data = sys.stdin.read(1)
    return bool(data)

i = 0
while True:
    i += 1
    if enter_pressed():
        print(i)

@Space_C0wb0y is right, a progress bar is a good solution. However, this demonstrates one way to do what you asked for. Some code pinched from here: Non-blocking read on a subprocess.PIPE in python

import fcntl, os, sys

# make stdin a non-blocking file
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

def enter_pressed():
    data = sys.stdin.read(1)
    return bool(data)

i = 0
while True:
    i += 1
    if enter_pressed():
        print(i)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文