I am writing a script to read my keystrokes and draw simple shapes in turtle.
To record keystrokes, I am using the keyboard module and I am using turtle for drawing.
I am getting struck due to the use of threading in the keyboard module.
What I am currently doing is-
I added hotkeys using keyboard.add_hotkey method.
If I am pressing a certain key, that letter is added to a list (named data) for later use.
When I press the combination of Ctrl+Shift+S, the save function is called. In the save function, a turtle window is instantiated, and the list data is popped one letter at a time. The shape is drawn according to the letter popped.
When the list gets empty, I save the drawing and close the turtle window.
The problem that I am facing is that once the save function is called, the program stops listening to other calls. It is perhaps due to the use of threads in keyboard module.
The code is attached here-
def start():
#turtle.mainloop()
s=turtle.Screen().setup( width = WIDTH, height = HEIGHT, startx = 0, starty = 0)
global t
t=turtle.Turtle()
turtle.ht()
t.ht()
def save():
start()
global t
global data
t.speed(0)
while data:
fun = data.pop()
if fun=='c':
draw_circle()
elif fun=='r':
draw_rectangle()
elif fun=='p':
draw_polygon()
elif fun=='h':
draw_hexagon()
elif fun=='t':
draw_triangle()
elif fun=='m':
draw_pentagon()
ts = turtle.getscreen()
ts.getcanvas().postscript(file="drawing.eps")
img = Image.open('drawing.eps')
img.save('drawing.png')
turtle.bye()
def push_fun(fun):
data.append(fun)
if __name__=='__main__':
keyboard.add_hotkey('ctrl+shift+s', save)
keyboard.add_hotkey('ctrl+shift+e', exit)
keyboard.add_hotkey('ctrl+shift+p', send_to_server)
# keyboard.add_hotkey('ctrl+shift+s', save, args=(data))
keyboard.add_hotkey('c', push_fun, args=('c',))
keyboard.add_hotkey('s', push_fun, args=('t',))
keyboard.add_hotkey('h', push_fun, args=('h',))
keyboard.add_hotkey('p', push_fun, args=('p',))
keyboard.add_hotkey('r', push_fun, args=('r',))
keyboard.wait()
After the save function is called, the program remains in the keyboard.wait() part but does not listen to any other key press.
you can end the wait by setting a key to press like:
keyboard.wait('space')
but keyboard.wait() without any keys given blocks all keystrokes forever
I'm not sure why you even put it there, I'm not even sure why it picks anything up at all, but I think you can do this:
if __name__=='__main__':
'''your keystrokes'''
while True: #or set a timer or something
pass
you didn't give me the whole code so I can't make sure it works
you also need to global the list data in the push_fun function
I hope it helps :)
Related
I was hoping someone could help me with this issue. I'm hoping it's fairly simple to fix, but I have been trying for a while to figure this out. I have trimmed my larger code to this, as I believe the issue here is the crux of the problem.
I have a raspberry pi and an external button. This is on python3 on Linux. I am using GPIOZero for the button. The code below I believe is simple to understand, but basically, I want a function to loop at all times. When I press a button I want another function to run, but only if a variable is a certain number. I describe what I ultimately want to happen in a comment below, but my code is unfinished and simplified just for this problem.
I only want button.when_pressed to work when timer = 0. The problem is, once the code naturally gets into the button.when_pressed function, it never "lets go" of the function again. When I successfully redefine the variable to timer = 1, it still prints button is pressed when I press the button. I don't know why. To me, it seems like it should only work once timer = 0.
Any suggestions? I think I have a misunderstanding of global variables I will plan on researching. I'm not sure if that's the issue. I have also tried using break and continue to try to get it "back on its loop" but that hasn't worked either. Also I want to use button.when_pressed instead of btn.is_pressed, because I want the program to only do something when I'm holding the button down once, and not loop when I'm holding it down. In this case, I want button is pressed to print a single time. If I did btn.is_pressed it would print button is pressed every two seconds, which I dont want.
Thanks for any help. I'm happy to learn.
#!/usr/bin/python3
from gpiozero import Button
from time import sleep
import time
button = Button(4)
timer = 0
def press():
print("Button is pressed")
global timer
timer = 1
def loop():
global timer
timer = 1
while True:
if timer == 0:
button.when_pressed = press
else:
loop()
sleep(2)
If you want to disable the callback you had set to button.when_pressed, you need to do another assignment with button.when_pressed = None. This is listed in the documentation:
when_released:
[...] Set this property to None (the default) to disable the event.
It's not exactly clear what behavior you want from your current code. If you want the button to be active for 2 seconds, then be deactivated indefinitely, you can use:
button.when_pressed = press
sleep(2)
button.when_pressed = None
There's no need for a loop, since you don't want to repeat anything.
If you only want the button to be active for a single button press, that needs to happen within 2 seconds, you could instead call button.wait_for_press(2). I hesitate to write a full block of code for that though, as the docs don't specify how a timeout is signaled (it might be by return value, or via an exception). I don't have a Raspberry Pi so I can't test myself, but you could try it out and see what happens.
Treat your whole piece of code as one "black box", ask yourself, what is the input/output? button press or timer mode? (because I don't quite understand what does timer variable mean in your code)
Your code implies timer mode is the top level input to control the flow,
while True:
if timer == 0:
button.when_pressed = press
else:
loop()
sleep(2)
Is it expected?
If you allow user to press the button at any time, suggest you make button press to be your top level input, change the logic to keep when_pressed callback always on, set flag once triggered, and then check if the button has been pressed and still is_pressed in your while loop.
pressed = False
def play_video_1():
pass
def play_video_2():
pass
def press():
print("Button is pressed")
global pressed
pressed = True
button.when_pressed = press
while True:
if pressed and not_playing_video2:
if is_pressed:
play_video_1()
else:
pressed = False
play_video_2()
else:
play_video_2()
I'm trying my hand at Pynput, and I'm starting off with creating a simple program to record the movements of a mouse, and then replay those movements once a button is clicked.
However, every time I click the mouse, it just starts to freak out and endlessly loop. I think it's going through the movements at a super high speed, but I eventually have to Alt-F4 the shell to stop it.
Any help would be appreciated.
import pynput
arr = []
from pynput import mouse
mou = pynput.mouse.Controller()
def on_move(x,y):
Pos = mou.position
arr.append(Pos)
def on_click(x, y, button, pressed):
listener.stop()
for i in arr:
mou.position = i
print("Done")
listener = mouse.Listener(on_move = on_move, on_click=on_click)
listener.start()
You have to be careful when using multiple threads (which is the case here, since mouse.Listener runs in its own thread). Apparently, as long as you are in the callback function, all events are still processed, even after you have called listener.stop(). So when replaying, for each mouse position you set, the on_move callback function is called, so that mouse position is added to your list again, which causes the endless loop.
In general, it's bad practice to implement too much functionality (in this case the "replaying") in a callback function. A better solution would be to use an event to signal another thread that the mouse button has been clicked. See the following example code. A few remarks:
I've added a few print statements to see what's happening.
I've added a small delay between the mouse positions to really see the playback. (NB: This also might make breaking out of the application a bit easier in case it hangs!)
I've changed a few variable names to make more sense. Calling an array "arr" is not a good idea. Try to use names that really describe the variable. In this case it is a list of positions, so I choose to call it positions.
I'm using return False to stop the mouse controller. The documentation states "Call pynput.mouse.Listener.stop from anywhere, raise StopException or return False from a callback to stop the listener.", but personally, I think returning False is the cleanest and safest solution.
import threading
import time
import pynput
positions = []
clicked = threading.Event()
controller = pynput.mouse.Controller()
def on_move(x, y):
print(f'on_move({x}, {y})')
positions.append((x, y))
def on_click(x, y, button, pressed):
print(f'on_move({x}, {y}, {button}, {pressed})')
# Tell the main thread that the mouse is clicked
clicked.set()
return False
listener = pynput.mouse.Listener(on_move=on_move, on_click=on_click)
listener.start()
try:
listener.wait()
# Wait for the signal from the listener thread
clicked.wait()
finally:
listener.stop()
print('*REPLAYING*')
for position in positions:
controller.position = position
time.sleep(0.01)
Note that when you run this in a Windows command prompt, the application might hang because you have pressed the mouse button and are then starting to send mouse positions. This causes a "drag" movement, which pauses the terminal. If this happens, you can just press Escape and the program will continue to run.
You got yourself an infinite loop. I think the listener you referred to in the on_click method might be null or undefined. Also according to some documentation I found you need to return false for the on_click method to stop listening
This is what I was looking at:
https://pythonhosted.org/pynput/mouse.html
I'm trying to make a game where the turtle needs to be able register and respond to user inputs (Simplified, a letter will appear and the user needs to click it on the keyboard. So if it shows "b" the user types "b"). I only added the letters a to f to make it simpler during the test. Each of them has a function that that'll execute when the letter has been pressed and the program is listening to it.
Everything worked out fine until I added a while function. Currently there is nothing in the while function (Asides from pass) but after I made it the code will no longer respond to user inputs. Could someone tell me how to fix this? My end goal is to have the program always listening for user input while a while loop runs and does its code. Below is my current code
import signal, turtle
def timeout_handler(signal, frame): # End of timer function
raise Exception('Time is up!')
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(10) # Number inside is how long the game will last.
def hit_a():
print("a registered")
def hit_b():
print("b registered")
def hit_c():
print("c registered")
def hit_d():
print("d registered")
def hit_e():
print("e registered")
def hit_f():
print("f registered")
turtle.onkey(hit_a, "a")
turtle.onkey(hit_b, "b")
turtle.onkey(hit_c, "c")
turtle.onkey(hit_d, "d")
turtle.onkey(hit_e, "e")
turtle.onkey(hit_f, "f")
turtle.listen()
while True:
pass
# Add program here
turtle.mainloop()
EDIT: I need a while code in there or at least a loop to keep a section repeating. In pseudocode it looks like this:
Several turtles each write one letter somewhere on the screen
Program waits for user to input a letter
Award/deduct points based on if they got it right or wrong
go back to line 2 if they got it wrong.
repeat
I plan on adding more stuff but I first need to get the base game going. The only way I know on how to code this is by using a while loop. But using one appears to be impossible since it prevents the program from listening to user inputs. Note that I need this all on turtle, not on the terminal (Command prompt if on windows) As I will colour code the letters to show which one to avoid and which ones to enter. How should I write this?
I also want to quickly mention that I have "avoided" this problem before. In the code below the program responds to user inputs when it's in the while loop. (Ignore the problems and the functions onkey() is assigned to. The idea is that the program responds while in the loop). However, I can't find out why in this code the program responds while in the loop but in the code above it doesn't register any user input
turtle.onkey(lower_game1_number, "s")
turtle.onkey(increase_game1_number, "w")
turtle.listen(xdummy=None, ydummy=None)
while True: # game1 is a turtle
game1.write(first_game_num_display,False,"center",("Arial",30))
game1_timer = random.randint(2,4)
time.sleep(game1_timer)
increase_game1_number()
game1.undo()
print(game1_timer)
mainloop()
Along with the "never use while True: in an event-based environment" advice that #martineau provided, you don't need to drag the 'signal' library into the code as you can handle that with an ontimer() event.
I need a while code in there or at least a loop to keep a section
repeating.
To address this, I've replaced my previous example code with a simple game that shows a letter and will keep moving it around the screen and changing it's color every two seconds, until you type that letter. Upon which, it will change to a different letter and continue on:
from turtle import Screen, Turtle
from random import shuffle, randrange
from itertools import cycle
WIDTH, HEIGHT = 600, 600
FONT_SIZE = 36
FONT = ('Arial', FONT_SIZE, 'bold')
LETTERS = list("abcdefghijklmnopqrstuvwxyz")
COLORS = ['red', 'blue', 'green', 'magenta', 'cyan', 'black', 'orange', 'gray']
def play():
global letter
if hit == letter:
letter = next(letters)
turtle.clear()
turtle.color(next(colors))
turtle.goto(randrange(FONT_SIZE - WIDTH/2, WIDTH/2 - FONT_SIZE), randrange(FONT_SIZE - HEIGHT/2, HEIGHT/2 - FONT_SIZE))
turtle.write(letter, font=FONT)
screen.ontimer(play, 2000)
letters = LETTERS
shuffle(letters)
letters = cycle(letters)
letter = next(letters)
colors = COLORS
shuffle(colors)
colors = cycle(colors)
hit = None
screen = Screen()
screen.setup(WIDTH, HEIGHT)
turtle = Turtle(visible=False)
turtle.penup()
for character in LETTERS:
def hit_character(character=character):
global hit
hit = character
screen.onkey(hit_character, character)
screen.listen()
play()
screen.mainloop()
Make sure to click on the window before typing to get it to listen for input.
Hopefully this will give you some ideas how to go about your more complex problem without using a while True: loop. Or at least show you how you might set up all your onkey() event assignments and handlers without rewriting the same code for every letter in the alphabet...
I am trying to write a python turtle program that behaves similarly to a regular event-driven program that uses a game loop. The program tries to mix mouse, keyboard and timer events as is as posted below.
My problem is that python doesn't seem to be able to mix the onkey() events with the ontimer() loop. When run, the program will animate the turtle and the onclick() event will work. The key press isn't even registered until the mouse is first clicked. Then, when the key is pressed to quit, I get a large list of errors in the shell. The bye() method seems to be terminating the program in a brutish fashion and not shutting down elegantly.
I think that I have the commands in the correct order.
Any suggestions will be appreciated!
import turtle
playGround = turtle.Screen()
playGround.screensize(800, 600, 'light blue')
bob = turtle.Turtle()
bob.color('red')
bob.pencolor('red')
bob.ht()
def teleport(x,y):
bob.goto(x,y)
def quitThis():
playGround.bye()
def moveAround():
bob.fd(10)
bob.rt(15)
playGround.ontimer(moveAround,30)
playGround.onclick(teleport,btn=1)
playGround.onkey(quitThis,'q')
moveAround()
playGround.listen()
playGround.mainloop()
One problem I see with your code is you need to keep the moveAround() from happening during the teleport() otherwise you get confusing visuals. I find with turtle that it helps to disable the event handler when inside the event hander and reenable it on the way out.
I believe the following will smooth out your events and allow them all to fire at the appropriate time. I've added a state variable to help control the activity:
from turtle import Turtle, Screen
def teleport(x, y):
global state
playGround.onclick(None) # disable handler inside handler
if state == "running":
state = "teleporting"
bob.goto(x, y)
state = "running"
if state != "quitting":
playGround.onclick(teleport)
def quitThis():
global state
state == "quitting"
playGround.onkey(None, 'q')
playGround.bye()
def moveAround():
if state == "running":
bob.fd(10)
bob.rt(15)
if state != "quitting":
playGround.ontimer(moveAround, 30)
playGround = Screen()
playGround.screensize(800, 600, 'light blue')
bob = Turtle(visible=False)
bob.color('red')
playGround.onclick(teleport)
playGround.onkey(quitThis, 'q')
playGround.listen()
state = "running"
playGround.ontimer(moveAround, 100)
playGround.mainloop()
when the key is pressed to quit, I get a large list of errors in the
shell. The bye() method seems to be terminating the program in a
brutish fashion
This is typical of turtle. If it really bothers you, see my answer to the question about Turtle window exit errors for one possible solution.
I want to change a label's text colour, wait a few seconds, then change it back when I press a key.
My end goal is making a full onscreen keyboard that will highlight the key that you pressed. However I can't get the function to pause between turning text blue, then back to black. I attempted using time.sleep(2), but it appears to do that at the start of the function, as opposed to the order I wrote it in.
from tkinter import *
import time
window = Tk()
window.geometry("1000x700")
LabQ = Label(window,text="Q",font=("Courier", 30))
LabQ.place(x=210,y=260)
def key(event):
LabQ = Label(window,text="Q",fg="ROYALBLUE",font=("Courier", 30))
LabQ.place(x=210,y=260)
time.sleep(2)
LabQ = Label(window,text="Q",font=("Courier", 30))
LabQ.place(x=210,y=260)
window.bind("<key>", key)
window.mainloop()
You have two problems. One is that you're not changing the color, you're creating an entirely new widget. To change the color you need to use the configure method on an existing widget.
Second, when you call sleep that's exactly what the GUI does -- it sleeps. No code is running and the screen can't be refreshed. As a general rule of thumb, a GUI should never call sleep.
The solution is to use use after to schedule the change for some point in the future:
def key(event):
bg = LabQ.cget("background")
LabQ.configure(background="royalblue")
LabQ.after(2000, lambda color=bg: LabQ.configure(background=color))
This example doesn't gracefully handle the case where you type the same key twice in under two seconds, but that's unrelated to the core issue of how to change the value after a period of time has elapsed.