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

Resubmission of questions:The drop_callback and drop_callback arguments in the add_button function are not called correctly. #2432

Open
nehonpa opened this issue Nov 21, 2024 · 5 comments
Labels
state: pending not addressed yet type: bug bug

Comments

@nehonpa
Copy link

nehonpa commented Nov 21, 2024


Version of Dear PyGui

Version: 1.11.1
Operating System: Windows 11

My Issue/Question

I want to know the correct usage of the drop_callback and drag_callback arguments, I have bound two functions in my code that do not see printed results, is this really called?

To Reproduce

Steps to reproduce the behavior:
run code

Expected behavior

According to the documentation, adding the drop_callback parameter and drag_callback should implement the control to follow my mouse to change the coordinate

Standalone, minimal, complete and verifiable example

# Here's some code anyone can copy and paste to reproduce your issue
import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport(title='123')
def function():
    print("run?")

def cc(a,b,c):
    print("no run?")
    dpg.set_item_pos("drag_1", dpg.get_mouse_pos())

with dpg.window(label="Tutorial",tag="main_window_1",horizontal_scrollbar=True,no_move=True,width=800,height=600):
    dpg.add_text("xxx", tag="tooltip_parent")

    dpg.add_button(label="but1",tag="drop_1",drop_callback=function)

    dpg.add_button(label="but2",tag="drag_tar",drag_callback=cc)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
@nehonpa nehonpa added state: pending not addressed yet type: bug bug labels Nov 21, 2024
@v-ein
Copy link
Contributor

v-ein commented Nov 21, 2024

To get a brief description of how to use drag and drop, run the demo and expand the "Drag & Drop" section and further the "Help" sub-section. Then, as @nvglucifer suggested in the older ticket, open demo.py and take a look at the implementation. It shows right away that you need a drag_payload that will be displayed while you're dragging the item.

Here's a corrected version of your example where both callbacks get called. I commented out set_item_pos because there's no "drag_1" item in your code.

import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport(title='123')
def function():
    print("run?")

def cc(a,b,c):
    print("no run?")
    # dpg.set_item_pos("drag_1", dpg.get_mouse_pos())

with dpg.window(label="Tutorial",tag="main_window_1",horizontal_scrollbar=True,no_move=True,width=800,height=600):
    dpg.add_text("xxx", tag="tooltip_parent")

    dpg.add_button(label="but1",tag="drop_1",drop_callback=function)

    dpg.add_button(label="but2",tag="drag_tar",drag_callback=cc)
    with dpg.drag_payload(parent=dpg.last_item()):
        dpg.add_text("Dragged data")

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()

A few words on payload_type:

  • it serves as a filter that determines what drop target will accept the dragged data (i.e. what items with drop_callback will be highlighted yellow and will get their drop_callback invoked).
  • payload_type on drag_payload determines what type of data is being dragged.
  • payload_type on a widget with drop_callback determines what types of data that drop callback accepts. If the data being dragged is of a different type, this widget will behave as a regular widget, i.e. you can't drop your data on this widget.
  • in other words, if you start dragging drag_payload that has payload_type="Apples", you can only drop it on widgets having payload_type="Apples".
  • payload_type is optional; if it's not specified at all, any widget with drop_callback will accept your payload (you can see it in the code snippet above).

P.S. @nvglucifer please take a look at this description of payload_type - will it be useful include this text into your PR?

@nehonpa
Copy link
Author

nehonpa commented Nov 21, 2024

Thank you very much for your reply, I probably understand how to use the drag_callback parameter. However, the drop_callback parameter is still unclear how it will be used. Could you give me another example?

@v-ein
Copy link
Contributor

v-ein commented Nov 21, 2024

drop_callback will be called when you drop something on that widget. In the example above, if you drag "but2" onto "but1" and drop it there, you'll see "run?" in the console.

@nvglucifer
Copy link
Contributor

@v-ein Yes, I will modify a point about payload_type being optional.

@nehonpa
Copy link
Author

nehonpa commented Nov 22, 2024

Thank you very much, I've studied for a long time and finally know how to use it easily

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: pending not addressed yet type: bug bug
Projects
None yet
Development

No branches or pull requests

3 participants