Dearpygui resizable table
Resizable table using main window resize as redraw trigger. Create an item_resize_handler that calls the table redraw, and bind the main window to it.
import dearpygui.dearpygui as dpg
import random
import string
DEBUG = 1
def generate_entries():
entries = []
for x in range(1, 10):
entry = {}
for y in ["A", "B", "C", "D", "E"]:
entry[y] = "".join(random.SystemRandom().choices(string.ascii_uppercase, k=6))
entries.append(entry)
return entries
def draw_entry_table(parent, entries):
if dpg.does_item_exist("entry_table"):
dpg.delete_item("entry_table", children_only=False)
table = dpg.add_table(
tag="entry_table",
header_row=True,
parent=parent,
resizable=True,
borders_outerH=True,
borders_innerH=True,
borders_outerV=True,
delay_search=True,
policy=dpg.mvTable_SizingFixedFit,
)
dpg.add_table_column(label="A", parent=table, init_width_or_weight=100, width_fixed=True)
dpg.add_table_column(label="B", parent=table, init_width_or_weight=100, width_fixed=True)
dpg.add_table_column(label="C", parent=table, init_width_or_weight=100, width_fixed=True)
dpg.add_table_column(label="D", parent=table, init_width_or_weight=100, width_fixed=True)
dpg.add_table_column(label="E", parent=table, init_width_or_weight=100, width_stretch=True)
if entries:
row_count = 0
for row in entries:
row_count += 1
with dpg.table_row(parent=table):
dpg.add_button(
tag="row" + str(row_count), label=row["A"], width=100, callback=cb_click_me, user_data=row
)
dpg.add_text(row["B"])
dpg.add_text(row["C"])
dpg.add_text(row["D"])
dpg.add_text(row["E"])
def cb_click_me():
print("I was clicked!")
def handler_cb_window_resize(sender, app_data, user_data):
(parent, entries) = user_data
draw_entry_table(parent, entries)
def main():
dpg.create_context()
if DEBUG:
dpg.configure_app(manual_callback_management=True)
dpg.create_viewport(title="TestPos", width=1500, height=800, decorated=True)
entries = generate_entries()
with dpg.window(tag="main", label="Window Test"):
dpg.add_menu_bar()
table = draw_entry_table("main", entries)
with dpg.item_handler_registry(tag="window_handler"):
dpg.add_item_resize_handler(callback=handler_cb_window_resize, user_data=("main", entries))
dpg.bind_item_handler_registry("main", "window_handler")
dpg.set_primary_window("main", True)
dpg.setup_dearpygui()
dpg.show_viewport()
if DEBUG:
while dpg.is_dearpygui_running():
jobs = dpg.get_callback_queue()
dpg.run_callbacks(jobs)
dpg.render_dearpygui_frame()
else:
dpg.start_dearpygui()
dpg.destroy_context()
if __name__ == "__main__":
main()
References: