How to make a drawing tool that follows mouse cursor? - python

I'm trying to make a app with Python that follows the mouse cursor. My attempts don't actually get close at all.
import turtle
o=1
Gps = turtle.Turtle()
for i in range(4):
while o==1:
I tried to make it to get mouse cursor position.
I have tried, but all I get is errors saying ? is not defined

Draw line only when you click mouse on screen
import turtle
def move_turtle(x, y):
turtle.setpos(x, y)
turtle.onscreenclick(move_turtle)
turtle.mainloop()
Draw when you drag turtle (you have to catch turtle, keep pressed mouse button and move mouse with turtle)
import turtle
def move_turtle(x, y):
turtle.setpos(x, y)
turtle.ondrag(move_turtle)
turtle.mainloop()
Maybe if you will have turtles with different colors or sizes then you can draw different lines.
To follow mouse when you don't press button would need to use Tkinter's functions hidden in turtle.
I think you should use other module to create drawing tool - tkinter, PyQt, PySize, wxPython, other GUI.

Related

How to get real mouse movement, not based on cursor position (python)

Im using windows and python
So I want to make a macro in a game, but the game always centers your mouse at the center of the screen. Every mouse movement algorithm only detects movements based on the difference between the previous cursor and the current cursor. Is there a way to detect mouse movement instead of cursor position?
Mouse movement is an event. So this event needs to be captured. Windows sends a WM_MOUSEMOVE message when the mouse moves. Various Python extensions can work with this event. E.g. tkinter does this as follows:
import tkinter as tk
root = tk.Tk()
def motion(event):
print('the mouse moved')
root.bind('<Motion>', motion)
root.mainloop()
When the mouse moves in the open window, it is reported that the mouse has moved. Are you looking for something like that?

Python: How to refresh the turtle window

is there some kind of method to refresh the turtle screen while keeping the turtle drawings and the pen having the same attributes, it's kind of like reloading a page but it still has the same content
There is a way to do that
and you can do it by:
t = turtle.Turtle()
t.clear()

Tkinter simpledialog breaks pygame.mouse.get_focused()

I'm writing a program that uses Pygame in conjunction with Tkinter. For some reason, showing one of Tkinter's simple dialogues messes up the pygame.mouse.get_focused() function. I wrote a smaller program to demonstrate the bug:
import tkinter as tk
from tkinter import simpledialog
import pygame
mainWindow = tk.Tk()
pygame.init()
screen = pygame.display.set_mode((500, 500))
while True:
screen.fill(pygame.mouse.get_focused() * 16777215)
pygame.display.update()
mainWindow.update()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.BUTTON_LEFT:
simpledialog.askstring(title="Test String", prompt="Gimme a string")
Before using any simple dialogues, this works as expected (the Pygame screen turns white if and only if the mouse is hovering over it). However, after opening the dialogue by left-clicking, closing the dialogue (either by entering text or clicking "cancel"), and right- or middle-clicking the Pygame screen, the Pygame screen turns white when hovering over any of the program's windows, not just the Pygame one. In this broken state, Pygame also thinks the mouse is hovering over its screen when the mouse is over the title bar, which it shouldn't.
Interestingly, this behavior does not occur if the dialogue is opened in a way other than clicking (by a key press, for example).
TL;DR
Pygame thinks the mouse is hovering over its screen after doing the following in order:
Opening a Tkinter simple dialogue by clicking
Closing the dialogue
Clicking in the Pygame screen without opening a dialogue (right- or middle-click)
Hovering the mouse over a Tkinter window
Looks like Pygame and Tkinter disagree over the event queue from the OS, so it's not easily fixable, aside from alternatives to Pygame and Tkinter.
https://github.com/pygame/pygame/issues/1995

Check if mouse is outside pygame window

I was running a game in pygame, and I discovered a flaw. The way I coded it, if the mouse went outside of the window, the game started to go around in circles. When you get pygame.mouse.get_pos(), it will return the last value the mouse was detected in the window, but other wise no indication that the mouse has left the window. So, is there any way to check if the mouse has stopped hovering over the pygame window, and has left it?
A simple answer to this would be to use the pygame.mouse.get_focused() function. This returns 0 when the mouse isn't focused on the screen. So if you wanted to check if the mouse was outside the window, you could simply do
mouseFocus = pygame.mouse.get_focused()
During the main loop and have an if statement checking if the mouse has left the screen.
if mouseFocus == 0:
print("mouse is out of screen.")
Hope this helps

How to make Python turtle window respond while waiting for further data

My code takes laptop's battery data and makes a graph with the data in Python turtle.
The code takes a datapoint every 2 minutes and prints it on the turtle screen. The problem is, while waiting for another datapoint, the turtle window doesn't respond and I can't make it active.
The turtle window works perfectly when I add turtle.done() in the end but the code doesn't continue after I close the turtle window, which I don't want, I want the window to be visible all the time.
Is there any good way to make the window "act" like with turtle.done() but it continues the loop?
If I'm understanding your needs correctly, then you might be able to use the turtle screen's ontimer() method. You want to do done() which turns control over to tkinter's event loop. But by having an ontimer() event, which reinvokes itself as the last thing it does, you can have event-friendly code constantly checking for more datapoints. Roughly:
from turtle import Turtle, Screen
def my_update():
# get the new data
# ...
# draw the new data
turtle.forward(...)
# reinvoke one-shot (5 secs. from now)
screen.ontimer(my_update, 5000)
screen = Screen()
turtle = Turtle()
# invoke one-shot (5 secs. from now)
screen.ontimer(my_update, 5000)
screen.mainloop()

Categories

Resources