如何打印倒计时计时器并同时接受用户输入(Python)?
澄清
这是我以前的问题的重新发布,因为我非常迫切希望收到我的问题的答案。我很新,如果这违反了任何规则,请告知我,因为如果是这样,我会删除这篇文章。
我想创建一个类似测验的程序,用户可以每秒看到倒计时计时器滴答作响,而他们可以随时输入答案。根据我有关此问题的上一篇文章,我尝试在代码中使用线程。这是我的代码的示例。
from threading import Thread
import time
import sys
def func1():
t = 10
for t in range (t,1,-1):
sys.stdout.write('\r' + str(t))
sys.stdout.flush()
time.sleep(1)
if __name__ == '__main__':
Thread(target = func1).start()
answer = input("\tEnter: ")
它确实可以运行,但问题在于用户输入被迫返回(\ r),而计时器不能正确删除10个中的“ 0”,这不是我想要的。这是输出:
如果您可以解决此问题的解决方案,那将是一个巨大的帮助。先感谢您。
Clarificaition
This is a repost of my previous question as I am extremely desperate to receive an answer to my problem. I am quite new and if this is against any of the rules, please inform me as I would remove this post if so.
I want to create a quiz-like program where the user would be able to see the countdown timer ticking down every second while they can input their answer at any time. According to my previous post regarding this question, I've tried using threading in my code. Here is a sample of my code.
from threading import Thread
import time
import sys
def func1():
t = 10
for t in range (t,1,-1):
sys.stdout.write('\r' + str(t))
sys.stdout.flush()
time.sleep(1)
if __name__ == '__main__':
Thread(target = func1).start()
answer = input("\tEnter: ")
It does function, but the problem is that the user input is forced to return back (\r) while the timer doesn't properly remove the '0' of the 10 which is not what I desire. Here is the output:
It would be a tremendous help if you could suggest a solution to this problem. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番混乱,我想到了。
如果您希望我对此进行编辑以使其在没有窗户的情况下使其正常工作,请告诉我。
希望这会有所帮助,欢迎您。 乇卩丨匚卄乂尺
(要阻止窗口完整屏幕,请更改(窗口).ATTRIBUTES(' - FULLSCREEN',true)为(窗口)。几何(500x500)或您想要的任何内容。
After some messing around, I came up with this.
If you would like me to edit this to make it work without the windows, let me know.
Hope this helps, and you're welcome. 乇卩丨匚卄乂尺
(To stop the window from being fullscreen, change the (window).attributes('-fullscreen', True) to (window).geometry(500x500) or whatever you wish.