简体   繁体   English

如何打印倒数计时器并同时接受用户输入(python)?

[英]How to print countdown timer and accept user input at the same time (python)?

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.它确实是 function,但问题是用户输入被迫返回 (\r),而计时器没有正确删除 10 的“0”,这不是我想要的。 Here is the output:这是 output:

不需要的输出

It would be a tremendous help if you could suggest a solution to this problem.如果您能提出解决此问题的建议,那将是一个巨大的帮助。 Thank you in advance.先感谢您。

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.如果您希望我编辑它以使其在没有 windows 的情况下工作,请告诉我。

import time
import tkinter
from tkinter import messagebox
from tkinter import *
from tkinter import ttk
from threading import Thread

def clear():
    print('\033[H\033[J', end='')
  
run = True

def timer():
    tim = int(input("Type how long the countdown should last in seconds: "))
    clear()
    count = 0
    while count < tim and run == True:
        clear()
        b1 = (tim-count)
        c = (str(b1),"second(s) left")
        win = Tk()
        win = Tk()
        win.attributes('-fullscreen', True)
        if run == False:
            win.destroy()
        Label(win, text= c,
        font=('Helvetica 20 bold')).pack(pady=20)
        win.after(1000,lambda:win.destroy())
        win.mainloop()
        time.sleep(1)
        count += 1
      
def take_input():
    inpu = input()
    #Your code
  
def time_input():
    global run
    while run == True:
        t1 = Thread(target=timer)
        t2 = Thread(target=take_input)
        
        t1.start()
        t2.start()
        
        t2.join()
        thread_running = False
        run = False
      
time_input()

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.尤婴丨齐占尺(要阻止 window 全屏显示,请将 (window).attributes('-fullscreen', True) 更改为 (window).geometry(500x500) 或您想要的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM