Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get size of node editor nodes with e.g. dpg.get_item_width #1239

Closed
christuart opened this issue Sep 13, 2021 · 16 comments
Closed

Cannot get size of node editor nodes with e.g. dpg.get_item_width #1239

christuart opened this issue Sep 13, 2021 · 16 comments
Projects

Comments

@christuart
Copy link

Version of Dear PyGui

Version: 0.8.64 on Python 3.7
Operating System: Scientific Linux 7.4

My Issue/Question

When using the node editor, nodes set their shape based on their child items. I want to be able to poll what the shape is, which will allow me to start positioning nodes sensibly when they are programmatically created.

Generally speaking, you can use dpg.get_item_pos, dpg.get_item_width/dpg.get_item_height and dpg.get_item_rect_min/dpg.get_item_rect_max to get this sort of information about any DearPyGui item.

However, for nodes, the width and height always return zero and the rect min and max are always equal. (They are also different from dpg.get_item_pos and I don't know if that is a bug or a feature based on the rect covering different elements than the pos...)

To Reproduce

Run the example below. Press the button in the window and observe the unhelpful values returned by the various relevant calls for properties of the node.

Expected behavior

I would expect dpg.get_item_pos and dpg.get_item_rect_min to return the pixel position of the top right of the node in the node editor coordinates, dpg.get_item_width and dpg.get_item_height to give the pixel width and height of the node and dpg.get_item_rect_max to equal dpg.get_item_rect_min + (dpg.get_item_width, dpg.get_item_height).

Standalone, minimal, complete and verifiable example

import dearpygui.dearpygui as dpg

with dpg.window() as window_id:

    dpg.set_primary_window(window_id, True)

    # Add a node editor with the simplest non-empty node
    with dpg.node_editor() as node_editor:
        with dpg.node(label="What width??") as node:
            with dpg.node_attribute():
                dpg.add_text(default_value="Empty attribute")

    # Add a function to print properties of the node
    def callback():
        print("Position (works):", dpg.get_item_pos(node))
        print("Width (broken):", dpg.get_item_width(node))
        print("Height (broken):", dpg.get_item_height(node))
        print("Rect min (has value):", dpg.get_item_rect_min(node))
        print("Rect max (has value but shouldn't equal rect min):", dpg.get_item_rect_max(node))
        print()

    # Add a button in a window to call that function
    with dpg.window():
        dpg.add_button(label="check width", callback=callback)

dpg.start_dearpygui()
@hoffstadt hoffstadt added this to Needs triage in Bug Tracker via automation Sep 14, 2021
@makew0rld
Copy link

I can confirm this is happening to me also. It happens with basic non-node elements as well, like windows. I could not get a correct size with get_item_width, get_item_height, or get_item_rect_size. It was always zero.

Version 0.8.64 with Python 3.9 on Arch Linux.

@hoffstadt
Copy link
Owner

This is an issue with terminology on our part. Width/height is the width and height you set. What you are looking for is get_item_rect_size(...).

This may seem odd but you have to remember that the width/height can also be set as pixels from the right/bottom.

For example, if you set the width to -50, the item will stretch to be 50 pixels from the right of its container. Aside from internally needing to keep up with "-50", users may also want to retrieve that value. So the ACTUAL size of an item is the rect_size.

There is some issues still with this for 0.8 but we are addressing them for 1.0 for Oct 12.

@hoffstadt hoffstadt removed bug labels Oct 1, 2021
@hoffstadt
Copy link
Owner

We will explain this further in the docs!

Bug Tracker automation moved this from Needs triage to Closed Oct 1, 2021
@makew0rld
Copy link

As mentioned in my comment, get_item_rect_size(...) doesn't work either, not even after being called after frames have been rendered. I also mentioned this in #1155 just now, so we could continue there if that's better.

@hoffstadt
Copy link
Owner

This should work in 1.0.0rc2?

@makew0rld
Copy link

makew0rld commented Oct 12, 2021

Ok, my bad. Thanks.

@christuart
Copy link
Author

Would I need to clone and build to test out 1.0rc2 or is there a way to get it from PyPI?

@hoffstadt
Copy link
Owner

@christuart I owe you several responses this week. I haven't been ignoring you, just swapped with the new 1.0 release coming today. Check here: https://github.com/hoffstadt/DearPyGui/discussions/1283

Its now 1.0.0rc5

@christuart
Copy link
Author

I have not felt ignored 🙂 thanks for the link, I hope I can try it out during this week

@iodbh
Copy link

iodbh commented Jan 20, 2022

This seems to still be an issue in both versions 1.1.0 on python 3.10 and 1.3.0 on python 3.9 (both on MacOS Catalina).

Running the code posted above by @christuart gives me 0 for both height and width, with an additional error :

Position (works): [100, 100]
Width (broken): 0
Height (broken): 0
Traceback (most recent call last):
  File "/...", line 22, in callback
    print("Rect min (has value):", dpg.get_item_rect_min(node))
  File ".../lib/python3.9/site-packages/dearpygui/dearpygui.py", line 822, in get_item_rect_min
    return internal_dpg.get_item_state(item)["rect_min"]
KeyError: 'rect_min'

@hoffstadt
Copy link
Owner

Fixed in release later today!

hoffstadt added a commit that referenced this issue Jan 20, 2022
@iodbh
Copy link

iodbh commented Jan 20, 2022

works like a charm, thank you @hoffstadt ! I'm impressed by your reaction time :)

@JCMSimon
Copy link

JCMSimon commented Nov 22, 2022

Doesent seem to work for me.
Running DPG 1.8.0 on python 3.9.5.
dpg.get_item_width,
dpg.get_item_height,
dpg.get_item_rect_min,
dpg.get_item_rect_max,
dpg.get_item_rect_size
all still return 0 for me

Edit:
When running in debug mode it sometimes works. No idea why

@Codingman11
Copy link

Same problem here. When I want to get the window's width it will return 0.

@JCMSimon
Copy link

JCMSimon commented Mar 9, 2023

Sorry for me being a bit late on this.
I went on to discuss this issue in the discord.
after adding a item at runtime, simply run dpg.split_frame() which will wait 1 frame, let the item render and assign its dimensions.
After that you can use the previously mentioned methods.

@Yrij-Zhavoronkov
Copy link

Yrij-Zhavoronkov commented Oct 3, 2023

Sorry for me being a bit late on this. I went on to discuss this issue in the discord. after adding a item at runtime, simply run dpg.split_frame() which will wait 1 frame, let the item render and assign its dimensions. After that you can use the previously mentioned methods.

dpg.split_frame()

This will hang the application if called before dpg.start_dearpygui().
If you attach a resize handler and wait for split_frame() in it, then the height and width of the components are still 0(

Any other suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

7 participants