0

I am currently trying to set up a BlueStacks macro with a JSON file from AutoHotKey. However, I need to know the coordinates of my pointer to set the BlueStacks macro. A python script would be preferred. this post's code didn't work, and I need more reputation to reply.

I have hardly ever used Python, so I don't know how to lol.

  • You can use a oneliner in PYAutoGUI, see [this post](https://stackoverflow.com/questions/44533241/how-to-print-out-live-mouse-position-coordinates-using-pyautogui) – Patrick F Jul 25 '23 at 06:53

1 Answers1

0

Edit: Updated code and formatting to be nice.

Edit2: Ironically, there is one small bug, clicking the button 'end' still logs the co-ordinates, which means your last clicked coordinates print statement on termination will always be where you clicked the button. The rest functions as expected and this function isn't in your described parameters so hope this works for you. Looking into this.

Edit3: Solved! I wish I could say I found the most graceful way to do it, but I couldn't, so I coded it in a 'dumb' way. I was hoping to just be able to phrase some variation of 'don't log co-ordinates when I touch this button' but I think the root command short-cuts such demands and makes it only see the root window even though clicks still interact with buttons, etc.

So instead, I quite simply made a second variable that stores the second to last click. When you click, previous_clicked_coordinates updates to the value of clicked_coordinates, then clicked_coordinates updates itself and is printed to terminal.

When you click the end button, quite simply, it terminates and instead of printing clicked_coordinates, prints previous_clicked_coordinates.

Dumb solution but it worked when the 'smart' ones did not, over and over again.

Code is edited to reflect the changes....

import tkinter as tk

def log_coordinates(event):
    x, y = event.x_root - root.winfo_rootx(), event.y_root - root.winfo_rooty()
    coordinates_label.config(text=f"X: {x}, Y: {y}")
    # Store the coordinates in a variable
    global clicked_coordinates, previous_clicked_coordinates
    previous_clicked_coordinates = clicked_coordinates
    clicked_coordinates = (x, y)
    print(clicked_coordinates)

def update_coordinates(event):
    x, y = event.x_root - root.winfo_rootx(), event.y_root - root.winfo_rooty()
    coordinates_label.config(text=f"X: {x}, Y: {y}")
    #print("Last motion event coordinates:", (x, y))
    # Store the coordinates in a variable
    global motion_coordinates
    motion_coordinates = (x, y)

def end_main_loop():
    root.destroy()

# Create the main application window and make it big
root = tk.Tk()
root.title("Mouse Coordinates")
root.geometry("300x300")

# Create a label to display the mouse coordinates
coordinates_label = tk.Label(root, text="Move the mouse to see coordinates", font=("Helvetica", 14))
coordinates_label.pack(padx=10, pady=10)

# Bind the mouse click event to the log_coordinates function
root.bind("<Button-1>", log_coordinates)

#this variable will store your clicked coordinates
clicked_coordinates = None
previous_clicked_coordinates = None
# This variable will store the latest motion event coordinates
motion_coordinates = None

# button to end this
end_button = tk.Button(root, text="End", command=end_main_loop)
end_button.place(x=135, y=250)

# Bind the motion event to the update_coordinates function
root.bind("<Motion>", update_coordinates)

# Start the main event loop
root.mainloop()

#print statement for your coordinates
print("Last clicked coordinates:", previous_clicked_coordinates)

This code:

  • Has two variables: clicked_coordinates and motion_coordinates

  • Has a label that displays the current co-ordinates of your mouse in the GUI.

  • Has a function def log_coordinates(event): that logs the coordinates of where clicked and stores this into a variable, ˚

  • Has a function def update_coordinates(event): that continuously updates the co-ordinates and sends this to your label. This is the sensor that allows you to monitor where you click before you click it in simple terms.

  • Has an end button that terminates the GUI/ program and prints out the last previous_clicked_coordinates variable stored.

  • Has vestigial sub-function print("Last motion event coordinates:", (x,y)) for tracking motion coordinates if you want, but it gives me anxiety having those constant print statements and I turned it off. This sub-function uses the otherwise vestigial motion_coordinates variable. Neither of these aspects are required to your use case but might be handy for whatever if you need to log motion instead of just static placements.

p.s.

I forgot to preserve the original text of my reply that was something like:

I did this with tkinter, but tkinter can have bugs without giving corresponding error reports that tell you anything.

If you try to re-create this program but leave out

def log_coordinates(event):
    **x, y = event.x_root - root.winfo_rootx(), event.y_root - root.winfo_rooty()**
    print("Clicked coordinates:", (x, y))
    # Store the coordinates in a variable
    global clicked_coordinates
    clicked_coordinates = (x, y)

and instead try something like:

def log_coordinates(event):
    **x, y = event.x, event.y
    coordinates_label.config(text=f"X: {x}, Y: {y}")**
    # Store the coordinates in a variable
    global clicked_coordinates
    clicked_coordinates = (x, y)

It's going to fail to work because it's tracking coordinates from elsewhere [???] like a label or something. I don't know exactly where elsewhere is, just that if you leave out the line that tells it to take coordinates from the window's root, it... wont. And it wont tell you that it's doing that so you just have to find ways to make meaningful information out to infer what is happening invisibly.

To that note: If you mess with tkinter because of this and have a stable state where all buttons function as anticipated; and then you suddenly don't-- and didn't change any sequence of statements or functions for buttons or GUI button definitions etc....

Just give your buttons extra negative space, some breathing room from the other buttons. IDK why, but you can be programming on the back end and something invisibly changes the button's perception of it's size and where clicks are occurring, or something like this. Anyways, just change where your buttons are in this case, even like 5-10 pixels will do it if by coordinates, if that fails do 20 (20 always solves in my experience, 10 usually does, 5 can but as frequently fails).

Anyways, hope this helps and also gets ahead of two common issues you could face while working with tkinter.

:)