dpg.get_mouse_pos() returned unintended position
atinfinity opened this issue · 4 comments
Version of Dear PyGui
Version: 1.11.1
Operating System: Ubuntu 22.04
Python 3.10.12
My Issue/Question
I hope to get clicked position on image window.
But, dpg.get_mouse_pos()
returned unintended position.
To Reproduce
Run the following script.
import dearpygui.dearpygui as dpg
import cv2
import numpy as np
def mouse_callback(sender, app_data):
mouse_pos = dpg.get_mouse_pos(local=True)
print(f"Mouse clicked at: {mouse_pos}")
width = 512
height = 512
img = np.full((height, width, 3), (0, 0, 255), np.uint8)
texture = (cv2.cvtColor(img, cv2.COLOR_BGR2RGBA).astype(np.float32)) / 255.0
dpg.create_context()
with dpg.texture_registry(show=False):
dpg.add_raw_texture(width=width, height=height, default_value=texture, tag="texture")
with dpg.window(label="Image", tag="window"):
dpg.add_image("texture", tag="image1")
with dpg.item_handler_registry(tag="image1_handler_registry"):
dpg.add_item_clicked_handler(callback=mouse_callback, button=dpg.mvMouseButton_Left)
dpg.bind_item_handler_registry("image1", "image1_handler_registry")
dpg.create_viewport(title='viewport', width=512, height=512)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
And, I clicked on the origin of the image window.
Expected behavior
I expected that dpg.get_mouse_pos()
return [0.0, 0.0]
.
Screenshots/Video
dpg.get_mouse_pos()
returned unintended position.
It appears to contain the value of border.
Mouse clicked at: [8.0, 8.0]
Why would you expect a (0, 0) there? get_mouse_pos
returns mouse coordinates relative to the viewport (local=False
) or to the active window (local=True
). Since you're binding the click handler to that red square, the "origin" I presume is the top left corner of that square, which is located at (window padding X, window paddingY) in window's local coordinates. The default padding is (8, 8) - exactly what you're seeing.
You can try to print that origin position like this:
print(f"Origin: {dpg.get_item_rect_min(sender)}")
@v-ein Thank you for your comment.
Why would you expect a (0, 0) there?
You can try to print that origin position like this:
I tried this. But, the following error occurs.
Mouse clicked at: [201.0, 175.0]
Traceback (most recent call last):
File "/home/user/dpg_example.py", line 8, in mouse_callback
print(f"Origin: {dpg.get_item_rect_min(sender)}")
File "/home/user/dpg-venv/lib/python3.10/site-packages/dearpygui/dearpygui.py", line 840, in get_item_rect_min
return internal_dpg.get_item_state(item)["rect_min"]
KeyError: 'rect_min'
My bad - it should have been dpg.get_item_rect_min(app_data[1])
since sender
is the click handler itself, and the clicked widget is passed via app_data
.
You'll also notice that get_item_rect_min
returns global coordinates. To get local ones, you can try get_item_pos
, but these will be relative to the window's outer rectangle, i.e. (0, 0) will be the top-left corner of the title bar, not of the window's client area. You can offset that by computing title bar height from font size and paddings, but it might be easier to use get_item_rect_min
and get_mouse_pos(local=False)
- from these, you can directly calculate mouse position within the image.
Yes, coordinate systems in DPG are a bit of a mess.
@v-ein Thank you for your help. I understood coordinate system.