Issue
I have been trying to make a user input with seconds together which counting down one by one till Zero. Sometimes somethings comes into my head but i take unexpected results. I am really wondering why the texts aren't being shown. Thanks for help from now.
import time
def timer():
for x in range(60,-1,-1):
print(x)
time.sleep(1.5)
input(f"Type code in {timer()} seconds : ")
Solution
What is wrong...
You are getting error because when you call input(f"Type code in {timer()} seconds : ")
, the program will run timer()
and try to get the returned value from it, then print it with f'Type code in {value} seconds : '
.
That is why you will get the count down of 60...0
on your screen, followed by Type code in None seconds :
, as timer()
return nothing(None
).
What to do...
Rendering a renewing display and trying to get user input at the same time in the command prompt is not ideal if not impossible. (Command prompt is not for fancy display.)
To achieve your goal, i suggest using an UI (user-interface), here is a simple UI to do it:
import tkinter as tk
class UI():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("200x50")
self.data = tk.StringVar()
self.label = tk.Label(text="")
self.entry = tk.Entry(textvariable=self.data)
self.label.pack()
self.entry.pack()
self.x = 60
self.count_down()
self.entry.bind("<Return>", self.end)
self.root.mainloop()
def count_down(self):
if self.x < 0:
self.root.destroy()
else:
self.label.configure(text=f"Type code in {self.x} seconds : ")
self.x -= 1
self.root.after(1500, self.count_down)
def end(self, keypress):
self.root.destroy()
app = UI()
value = app.data.get()
# do whatever you want with value
print('value : ',value)
Answered By - ytung-dev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.