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

发布于 2025-01-23 10:05:54 字数 797 浏览 0 评论 0原文

澄清

这是我以前的问题的重新发布,因为我非常迫切希望收到我的问题的答案。我很新,如果这违反了任何规则,请告知我,因为如果是这样,我会删除这篇文章。

我想创建一个类似测验的程序,用户可以每秒看到倒计时计时器滴答作响,而他们可以随时输入答案。根据我有关此问题的上一篇文章,我尝试在代码中使用线程。这是我的代码的示例。

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:

Undesired Output

It would be a tremendous help if you could suggest a solution to this problem. Thank you in advance.

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

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

发布评论

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

评论(1

醉南桥 2025-01-30 10:05:54

经过一番混乱,我想到了。
如果您希望我对此进行编辑以使其在没有窗户的情况下使其正常工作,请告诉我。

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()

希望这会有所帮助,欢迎您。 乇卩丨匚卄乂尺
(要阻止窗口完整屏幕,请更改(窗口).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.

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.

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