>>> pyautogui.displayMousePosition
<function displayMousePosition at 0x044538E8>
I want the output to display the mouse's coordinates, not some funky stuff.
You are not executing the function, add () behind, else you make it a callback.
pyautogui.displayMousePosition()
Will display the mouse X and Y along with its current position pixel color.
More info on parentheses here and here.
Related
I am trying to automate some work using python code and the function "pyautogui" but I need to find a way to detect colours where the mouse is on the screen.
Anyone has any solutions?
Thanking in advance.
import pyautogui
while True:
x, y = pyautogui.position()
px = pyautogui.pixel(x, y)
print(px)
You can call pyautogui.pixel(x, y) to obtain the (red, green, blue) value of the pixel where the mouse is. On macOS unfortunately, the mouse cursor itself is included in the screenshot. You will have to call x, y = pyautogui.position() to obtain the x, y coordinates, then call pyautogui.moveRel(50, 0) to move the mouse cursor away, then call pyautogui.pixel(x, y) to get the color of the pixel of where the mouse cursor was.
This will hopefully be corrected on macOS in a future version of PyAutoGUI.
I want to know how to get the x-coordinate position or the y-coordinate position of the mouse individually on pygame.
Like just the x and just the y. I think It would use
pygame.mouse.get_pos
Pygame doesn't have an API that will get you only one coordinate, you always get both. But it returns them in a 2-tuple, so you can index to get just one value if you want to:
x = pygame.mouse.get_pos()[0]
If there's any chance you might need the y coordinate as well, it might make sense to unpack as normal anyway, and just ignore the y value in the part of the code where you don't need it:
x, y = pygame.mouse.get_pos()
# do stuff with x, ignore y
if something_rare_happens():
# do stuff with y too
It might even be clearer to do the unpacking even if you'll never use y, but that's really up to you.
Write a function called connectTheDots that takes in a list of tuples as its input and an optional color input as well. The default color value should be black. Each tuple is a coordinate pair (x, y) for a turtle. The function will have the turtle trace out a picture by starting at the first coordinate and then moving to each coordinate in turn.
Your function should do the following:
a. Create a turtle, setting the turtle’s color and speed appropriately
b. Check if the input list is empty: if it is empty then nothing else should happen!
c. Without leaving a line behind, move the turtle to the first location given in the list. Then start leaving a line again. Note: recall how to pull values out of a list, and also know that the goto method can take a single (x, y) tuple as its input: myTurtle.goto( (25, 25) ) will move myTurtle to x = 25 and y = 25.
d. After the turtle is at the starting coordinate, move it to each coordinate in the list in turn.
This is what I have been able to do so far:
def connectTheDots(list1, color ="black"):
myTurtle = turtle.Turtle()
myTurtle.speed(1)
myTurtle.goto(list1[0])
for x,y in list1[1:]: #I'm unsure if this is correct
myTurtle.goto(x,y)
You have most of what you need but are probably making it more complicated than needed and are missing some small details.
For step "a" you need to explicitly set the color (you passed it in just fine). You are probably better off using a symbolic speed instead of a numeric one.
For step "b", if you have a proper for ... in loop, you don't need to explicitly check if the list is empty as the loop won't run if it is. Your splitting off the first item myTurtle.goto(list1[0]) works against you here as there may not be one, causing an IndexError.
For step "c" you need to add another command. Turtles start life in the center of the screen with their pens down. You need to raise the pen up after creating your turtle. But you don't need to explicitly move to the starting position, let your loop handle that.
The trick we'll use for step "c" and step "d" is to put the pen down after the goto() in the loop. The first time, this actually puts the pen down, after that, it's a harmless no-op:
import turtle
def connectTheDots(coordinates, color="black"):
myTurtle = turtle.Turtle()
myTurtle.speed("slowest")
myTurtle.color(color)
myTurtle.penup()
for coordinate in coordinates:
myTurtle.goto(coordinate)
myTurtle.pendown() # redundant after first iteration
dots = ((34, 56), (100, 240), (230, 105), (34, 56))
connectTheDots(dots, "green")
turtle.done()
If it bothers you that we're putting the pen down unnecessarily in the loop, then we can replace myTurtle.pendown() with:
if not myTurtle.isdown():
myTurtle.pendown()
On GLUT glutkeyboardfunc it returns 3 values I understand the char parameter but don't understand the other 2 are. Want do these parameters mean?
The additional params are the x and y callback parameters indicate the mouse location in window relative coordinates when the key was pressed
If in gnuplot I type plot(x**2), and get a plot of this function, in the lower-left corner of the plot I can see the coordinates corresponding to the position of my mouse pointer.
What I would like to know is if there is a way to "snap" the pointer to the function's graph (or rather the crosshairs whose coordinates are shown, making them share the same x-coordinate as the mouse-pointer).
The end effect will be that as I move my mouse left-to-right along the plot, a crosshair which has the same x-value as the pointer will be shown directly on the function's graph, and the current coordinates of this crosshair will be printed somewhere (e.g. in the lower-left corner of the plot). In other words, the printed crosshair coordinates would always be (x, f(x)) for some x value.
While this would be useful for functions (x, f(x)), please note that gnuplot can also plot parametric functions as well as 2D and 3D surfaces, so this functionality would be of limited use. Note also that you can already output tables with set table and output values to the console with for and print.
If you do need interactivity, here is an MWE that outputs pairs of (x, f(x)) according to the mouse pointer X position if you click into the screen, as a label on the screen as well as via print to the console (remove as necessary).
#!/usr/bin/gnuplot -persist
## this binds commands to the mouse click that uses the MOUSE_X variable
## to do what you want
bind all "Button1" \
'result=sprintf("(x, f(x)) = (%g, %g)", \
MOUSE_X, f(MOUSE_X)); \
set label 1 result at graph 0.05, graph 0.05; \
print result; replot'
f(x) = x**2
plot f(x)
## the pause is needed only to keep gnuplot running,
## so you see the print output
## the label works without the pause
pause mouse