I am making a questionnaire using python and want to have a visible timer on the questionnaire. The code I used for the timer is below but when it is counting down, it creates a new line and would not let the rest of my questionnaire run. Please help!
import os
import time
s=59
m=5
while s<=59:
os.system('cls')
print ( m, "Minutes", s, "Seconds")
time.sleep(1)
s-=1
if s==0:
m-=1
s=59
Seems there are two problems:
Your timer loop blocks the rest of your application
Printing the current time gives a newline and makes the screen scroll
To solve 1 use threads or processes. Timer and questionaire are in a different thread.
To solve 2 you could use escape sequences to control the screen. But since your questionaire will have to scroll without scrolling your timer, this really begs for a GUI. Tcl/Tk is the GUI library that comes with Python. It will be the easiest to use that, I expect.
Related
When I use the function time.sleep() in the terminal, it is no problem.
When I use PyQt and want to use a label, it crashes and/or only shows the last number.
By the way: I want a function, that counts e.g. a year from 2020 to 2030 in various speed, that the user can change and the year should be shown in a label.
Thanks a lot for your help.
# timer that counts in the future with various speed // still crashing
def timer(self):
x=datetime.datetime.now()
z=x.ctime()
self.ui.labelDateTime.setText(z)
var=x.year
while True:
if var==2030:
break
else:
var+=1
y=x.replace(year=var)
z=y.ctime()
self.ui.labelDateTime.setText(z)
time.sleep(0.5)
print("You are dead")
You should call QtCore.QCoreApplication.processEvents() within your for loop to make the Qt's event loop proceed the incoming event (from keyboard or mouse or sleep).
Although calling QtCore.QCoreApplication.processEvents() works , I have read in many places on the web that it should be a last resort. Unfortunately none of the sources clearly explain why -- but see for example
How to make Qt work when main thread is busy?
Should I use QCoreApplication::processEvents() or QApplication::processEvents()?
How to implement a QThread that runs forever{} with a QWaitCondition but still needs to catch another Slot while doing that
http://qt-project.org/forums/viewthread/22967
So it does seem allowed, but in general, it seems to be better design to use QTimer or QThread.
I want to make a little command line music player based on the python module "mp3play".
I want to check regularly, if a song has stopped playing (and eventually start a new song), but the user should be able to type new commands during that time (like pausing the music).
Therefor i tried to use threading.Timer for that. However, it gives me an error if i am inside the function that was called using the timer. the error does not occur when the function was called normally. Heres my (reduced) code:
from threading import Timer
global currentmusic
def rep():
b = currentmusic.isplaying() #this is where the error occurs
if b:
print "Music is playing"
else:
print "Music has stopped"
t=Timer(5.0,rep) #repeat every 5 seconds
t.start()
currentmusic=playrandomfile() #loads a song and starts playing it
rep() #call the first time
When rep() is called the second time, it gives me an MCI error in the function isplaying(), saying that it cannot read the device. My questions:
Am i making a mistake with the way the threading.Timer works? (and how can i fix it?)
Is there another way than threading.Timer to achieve the stuff i want?
My thoughts so far were, that it could be a problem to access currentmusic from another thread, but i am not sure. Also i dont know how to avoid it.
Thx for helping
Ive used mp3play in some projects, and it works fine for me.
IMO recursion threading is the problem.
Just remove the threading timer and leave the rep function call, it wont lag any pc.
You should use threading only for raw_input.
I'm working on a script that grabs control of my mouse and runs within a simple infinite while loop.
def main():
while True:
do_mouse_stuff()
Because of the mouse control, it's a pain to click on the python window and hit ctrl-c, so I've been looking for a way to implement a global hotkey in windows. I'm also a relative Python noob so I've probably missed an obvious answer. Stuff I've found:
pyhk - the closest I've gotten, but this module does nasty things to my computer for some reason (probably something I'm doing wrong), it introduces major mouse lag, complete input lockout, all kinds of stuff I'm not smart enough to deal with.
pyHook - Followed the tutorial, works fine but the infinite running message pump and my while loop appear to run exclusively and I haven't figured out how to make it work.
Another Method - I found this method as well, but I have the same problem as pyHook, the try loop and my while loop cannot coexist.
I've tried to figure out how to integrate my loop into these examples rather than maintaining a separate loop but I've not been able to make that work, again likely due to my noobishness. Would someone be able to straighten me out on how to make this work?
Perhaps using msvcrt? I'm not sure if it's "global", and I can't test it right now, unfortunately, but here's an example of detecting the Escape key (taken from this question), integrated with your keyboard stuff:
import msvcrt
def main():
while True:
do_mouse_stuff()
# Check if `Esc` has been pressed
if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
aborted = True
break
I'm working on a Python program that is to use a timer. It is going to be a memory game where a word pops up and a timer counts to 5 and after 5 seconds the words disappears and you have to type the word from memory. I have already searched around a little and have seen things that time the execution time of a program but don't do anything that I want it to. Does anyone have a code to do such a thing? I'm using Python 2.7.3 on a Windows Vista computer.
Thanks in advance, Sid
time.sleep(seconds) will sleep for some seconds and you can print a message
eg:
import time
for i in range(5,0,-1):
print "%d Mins Left"%i
time.sleep(60)
however if you want to update the previous time print you will need to look at the curses library (if you are using the terminal) or something like wxPython or pygame or any of the myriad of other graphical libraries for python if you want to use graphics
Use the time module:
from time import time
start = int(time())
topass = 5
while int(time()) - topass < start:
pass
print topass, "seconds have passed"
If it is going to have a GUI, you could build this application easily using Tkinter using the after method of tkinter widgets.
If you're not going to have a GUI, you'll probably need 2 threads (1 to do the timing, 1 to accept input from the keyboard).
I have some code:
l1 = clutter.Label()
l1.set_position(100,100)
for i in range(0,10):
l1.set_text(str(i))
time.sleep(1)
That is designed to show a count from 1 to 10 seconds on the screen in clutter, but I'm getting a strange error. When I run the script normally the screen runs as it should do, but there is no text displayed until 10 seconds are up. However, When I run with breakpoints in pdb the text shows up just fine.
I'm also getting a strange error at the start of the program:
do_wait: drmWaitVBlank returned -1, IRQs don't seem to be working correctly.
Try adjusting the vlank_mode configuration parameter.
But I don't see why that would affect the code out of break points but not in breakpoints.
Any help would be greatly appreciated.
Not sure if you've already figured out the answer to this but:
The reason you are having this problem is because you are blocking the main thread (where all the drawing occurs) with your time.sleep() calls, preventing the library from redrawing the screen.
E.g. your code is currently doing this:
Clutter redraws the screen.
You loop over ten seconds and change the text ten times.
Clutter redraws the screen.
If you want to queue something on a timer, you should look into gobject.timeout_add.
Have you tried posting (or searching) on the Clutter mailing list? Here's someone who got the same message about drmWaitVBlank for example.
My guess is most people on SO wouldn't be familiar with solving Clutter problems. I know I'm not :)