I'd like to program a graphical clock, which has three pointers, one for the hour, another for the minute and the last for the second. The time is synced with my local PC time, so I need the clock pointers infinitely move like a real clock, unless I end the program. How can I determine the moving clock pointers? Is there a function for that? I looked it up in Turtle's documentation, but found none. Maybe I've missed something. Here's my yet incomplete code:
import time
import os
import cursor
import turtle
cursor.show()
s = turtle.Turtle()
turtle.bgcolor('Black')
hour = int(time.strftime('%H'))
minute = int(time.strftime('%M'))
second = int(time.strftime('%S'))
s.pensize(5)
s.pencolor('Red')
s.circle(200)
s.penup()
s.goto(0,200)
s.pendown()
s.left(90)
s.forward(100) #how can I make this rotate 30 degrees???
turtle.done()
Thank you in advance :))
Related
my question is actually quite easy but I get stuck with this issue.
I want to set timer to 2 second and then check if the mouse is still in the same poit as previous.
example: I detect the point of the mouse as (250, 500) and then put timer to 2 secs and check again where is the pointer now.
would appreciate your help :)
you can use time.sleep in cycle for
import time
for i in range(0,2):# range: 0,1 = 2 iteration = 2 sec
time.sleep(1)
So, this piece of code:
import pyautogui
pyautogui.PAUSE = 0
pyautogui.MINIMUM_SLEEP = 0
pyautogui.MINIMUM_DURATION = 0
pyautogui.moveTo(100,100,duration=1)
Makes the mouse takes 20+ second to reach its destination. The cause is the MINIMUM_SLEEP value, no matter the duration added it will take the same time (ages). Is there a way around this? - Is there any way to make the mouse movement smooth and still set the duration?
I am very surprised I haven't seen any comments on this. Thanks.
I need to run a code for hours, and the computer I am working with has a (forced and unchangeable) screensaver policy. (It locks after 10 minutes). What can I do to prevent it? Is there any line of code to prevent that?
This worked for me. I'll just leave it here so people can use it.
import ctypes
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002) #this will prevent the screen saver or sleep.
## your code and operations
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000) #set the setting back to normal
You can prevent screen saver by moving mouse cursor on a fixed period of time, below function can handle cursor moving.
import win32api
import random
def wakeup():
win32api.SetCursorPos((random.choice(range(100)),random.choice(range(100))))
This is a coded solution that you can place in your program (also works for Mac users):
pip3 install pyautogui
https://pypi.org/project/PyAutoGUI/ (Reference)
import pyautogui
import time
def mouse_move():
while True:
pyautogui.moveTo(100, 100, duration = 1) # move the mouse
time.sleep(60) # Every 1 min
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()
Or, without the while loop, run it when required if your program is already within a while loop:
def mouse_move():
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()
i am supposed to draw a clock in python without using any modules that need downloading like turtle module, rather id have to use the stddraw module. The clock would also have to give the current time in hours, minutes, and seconds represented on the clock. I am struggling to understand how i'm supposed to go about doing this since i havent done any drawing or anything before so this is really new territory in terms of programming. Any ideas on how to go about doing this or advice is greatly appreciated!
without using any modules that need downloading like turtle module,
rather id have to use the stddraw module
As #PurpleIce starts to get at, you've got this backward. The turtle module comes with Python, the stddraw module needs to be downloaded (from Princeton.)
Your question has inspired me to see if it is possible to make a minimalist working clock using Python turtle:
from time import localtime
from turtle import * # avoid wildcard imports like this
ATTRIBUTES = ['tm_hour', 'tm_min', 'tm_sec']
def tick():
record = localtime()
hands['tm_hour'].seth(record.tm_hour % 12 * 30 + record.tm_min / 2 + record.tm_sec / 120)
hands['tm_min'].seth(record.tm_min * 6 + record.tm_sec / 10)
hands['tm_sec'].seth(record.tm_sec * 6)
ontimer(tick, 1000)
mode("logo") # make 0 degrees be straight up the page
hands = {}
for size, attr in enumerate(ATTRIBUTES, start=1):
hands[attr] = Turtle('triangle')
hands[attr].shapesize(1 / size, size * 10)
tick()
mainloop()
Hopefully, this will give you insight on how to begin building your own clock using the stddraw module:
I am testing out pyglet for usage in a larger project, and apparently pyglet recommends/wants you to use it's own loop (with pyglet.app.run())
This is a something I don't want, for reasons of compatibility of other packages and also to not have to rewrite the entire program structure.
Here I have prototype code stuck together from different parts and tutorials and docs.
It runs for 5-15 iterations and then just freezes, not printing anything and also not doing any draw updates.
from __future__ import division, print_function
import sys
import pyglet
window = pyglet.window.Window(800, 800, resizable=True)
window.set_caption('Pyglet Testing')
window.flip()
image = pyglet.resource.image('Sprites/scout.png')
def draw(dt):
image.blit(700-dt, 400)
while not window.has_exit:
dt = pyglet.clock.tick()
window.dispatch_events()
window.clear()
draw(dt)
window.flip()
print(dt)
My suspicion is that I have done nothing to catch events and handle them, so at a certain point it just overflows with events and blocks the whole thing. I couldn't understand how to do this however, and getting overflowed with events in under 1 second seems a bit much.
Any help?
Basically what you are doing is sending as many image.blit(...) commands to the window, until the pc probably can't handle it anymore.
For instance, if you change your code like this:
add this code:
import time
from time import sleep
change code:
def draw(dt):
image.blit(700-dt, 400)
sleep(0.1) #insert this line
When executing the modified code, you will notice that it does not freeze and that the output dt is around 0.11 seconds, which is the number of seconds since the last "tick" = the time slept (0.1 second) + the remainder time (clear window, display new frame ...)