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.

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Surely, your code does contain all the requirements I’ve expected, great job! However, I have three major issues that might burden you a bit more ( ´~`ヾ). Firstly, I use a MacBook so it is almost impossible for me to execute window-limited codes (I’ve tried using VsCode but somehow, it doesn’t seem to work). Moreover, I use a 3rd party website called Replit (and recently started using VsCode) which is quite limited in its functionality. Lastly, I am limited to producing this effect within a CUI but without the aid of a GUI which once again, restricts the range of codes you can use.
I am really sorry to request such work with immense restrains but I would like to humbly ask you to once again, pull me out of this well. Thanks again :0
(Sorry about the late reply, I've been busy) This was just something I quickly put together. I will work on something a little bit better. And no problem.
I have come to the conclusion, that python is limited in what you can do, and what you have said limits it even more. I am also currently working on a project so I cannot find a solution. I am greatly sorry. You can do what you have said in Javascript and many other coding platforms. It will also work if you make a website or HTML page. Sorry for the inconvenience.
Ahh, it’s fine as I’ve also realised that such a result would be impossible within the limits of a CUI. Instead, I plan to use a GUI for this specific task and await a satisfactory result. Nevertheless, I am substantially pleased that you’ve provided your precious time to suggest to me some great advice, thanks so much. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.