Resubmission of questions:The drop_callback and drop_callback arguments in the add_button function are not called correctly.
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()
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_callbackwill be highlighted yellow and will get theirdrop_callbackinvoked). payload_typeondrag_payloaddetermines what type of data is being dragged.payload_typeon a widget withdrop_callbackdetermines 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_payloadthat haspayload_type="Apples", you can only drop it on widgets havingpayload_type="Apples". payload_typeis optional; if it's not specified at all, any widget withdrop_callbackwill 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?
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?
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 Yes, I will modify a point about payload_type being optional.
Thank you very much, I've studied for a long time and finally know how to use it easily