DearPyGui icon indicating copy to clipboard operation
DearPyGui copied to clipboard

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

Open nehonpa opened this issue 1 year ago • 5 comments


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 avatar Nov 21 '24 10:11 nehonpa

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?

v-ein avatar Nov 21 '24 12:11 v-ein

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?

nehonpa avatar Nov 21 '24 12:11 nehonpa

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.

v-ein avatar Nov 21 '24 13:11 v-ein

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

nvglucifer avatar Nov 21 '24 14:11 nvglucifer

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

nehonpa avatar Nov 22 '24 02:11 nehonpa