Trying to find a timer in Python - python

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

Related

How to disable or ignore python input in the console? on Mac

I see a lot of answers to this question online, but the only solution I have found uses the msvcrt module that (as I understand it) is only available for Windows.
I am making a simple python console game and I want to stop the user from typing anything while the application is loading or playing a simple animation as it tends to break the program.
Example:
import time
#disable input here
print('hi')
time.sleep(3)
print('3 seconds have gone by')
#enable input
I don't want to let the user roll their face over the keyboard and make everything messy and ugly. I know mac is very strict about this kind of stuff, is it even possible?
From the solution you linked I can see that you are only needing the msvcrt.getwch() function, so you could use the one from the getch module i.e. getch.getch().

Sending keyboard input to windows lock screen

As it sounds I wanted to create my own kinda smart assistant that can actually unlock my pc (Cortana can't and I could not find any good solution for this).
for now, I have tried multiple ways including messing with winlogon.exe and MSGINA.dll. After about 3 hours of search, I haven't found anything that could actually unlock my pc, and I saw some programs that said that they have succeeded in unlocking their pc while messing around with this files but in reality, it just prevents the locking mechanism and it is not my intention.
right now I have seen some videos about using pyautogui and keyboard to control keyboard and mouse movement and I wrote a small script that can "unfold" the windows lock screen cover (where the time and date is displayed) but nothing more than that (it is not working at all)
import keyboard
import time
for i in range(10):
print 10 - i
time.sleep(1)
keyboard.send('enter')
time.sleep(2)
keyboard.write("password1")
keyboard.send("enter")
also tried keyboard.press_and_release and it doesn't do anything.
how can I send any keyboard input to the lock screen?
and if it is not possible, do you have any other suggestions?
EDIT: I am Working on windows 10 build 1803 and python 2.7

(python) Different behaviour when called using threading.Timer

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.

Blender frozen in python script?

I am new to both blender and python.
I tried to manipulate some properties of an object via python script in script console of blender.
What I don't understand is I can do it in this way.
bpy.data.object['Cube'].rotation_euler.x+=1
but when I put it in a loop.
import time
i=1
while i<100:
i+=1
bpy.data.object['Cube'].rotation_euler.x+=1
print('run once')
time.sleep(5)
Blender freezes without any output of 'run once'.
Would someone please tell me what is wrong with this code.
Your script isn't freezing, blender just isn't getting a chance to update during the loop.
The time.sleep(5) command sleeps for 5 seconds, being run 100 times means the script takes 8 minutes to run at which stage blender updates it's interface again.
You may want to look at a modal operator - there are several samples within the python templates available in blender's text editor.

Beginner at Python (Timer help)

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.

Categories

Resources