hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en
MIT License
13.03k stars 678 forks source link

Can't use set_y_scroll for listbox #2298

Open Nonoreve opened 7 months ago

Nonoreve commented 7 months ago

Version of Dear PyGui

Version: 1.9.1 Operating System: Manjaro (in virtual env)

My Issue/Question

I want to a listbox to focus a particular line. But using set_y_scroll() gives the message :
Incompatible type. Expected types include: mvWindowAppItem, mvChildWindow The listbox does indeed have a scrollbar, what prevents it from being scrolled programmatically ? Is there an other way to do it ?

To Reproduce

Run code below Click on the button

Expected behavior

The scrollbar should move to the desired position.

Standalone, minimal, complete and verifiable example

import dearpygui.dearpygui as dpg

i = 0

def callback(sender, app_data, user_data):
    global i
    print(i)
    # ~ dpg.configure_item(duplicate_documents_listbox, track_offset=i)
    dpg.set_x_scroll(duplicate_documents_listbox, i)
    i += 0.1

dpg.create_context()
dpg.create_viewport(width =500, height=200)
dpg.setup_dearpygui()

with dpg.window(no_close=True, modal=True, width =500, height=200):
    items = [("a"), ("b"), (1), ("a"), ("42"), (True), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    duplicate_documents_listbox = dpg.add_listbox(label="", items=items, default_value='', num_items=3, tracked=True)
    dpg.add_button(label="Move scrollbar", callback=callback)

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
v-ein commented 7 months ago

The way it's currently implemented in DPG, every widget requires its own implementation of "set/get scroll" functions. Nobody has ever implemented it for listboxes.

I have a fix that might eventually get into the community repo, which improves implementation of the scroll functions and, among other things, should make it easy to enable them on listboxes. This fix has to wait some other PRs to be merged first, so it's not going to happen in near future.

In the meantime, you can implement your own listbox by placing selectable's into a child_window. It's pretty easy to do, and you can control scrolling like on a regular child window.

Also take a look at this implementation and other files in that folder - maybe you can just use them as is. https://github.com/my1e5/dpg-examples/blob/main/listbox/listbox_custom.py