容器和上下文管理器

我们添加了上下文管理器作为大多数容器项的帮助器。

参见

了解更多详细信息 集装箱堆叠

核心命令

上下文管理器

add_table

附表(...):

add_table_row

with table_row(...):

add_window

带窗口(...):

add_menu_bar

with menu_bar(...):

add_child

带着孩子(...):

add_clipper

使用剪刀(...):

add_collapsing_header

with collapsing_header(...):

add_colormap_registry

with colormap_registry(...):

add_group

使用组(...):

add_node

使用节点(...):

add_node_attribute

with node_attribute(...):

add_node_editor

with node_editor(...):

add_staging_container

with staging_container(...):

add_tab_bar

with tab_bar(...):

add_tab

带制表符(...):

add_tree_node

with tree_node(...):

add_tooltip

使用工具提示(...):

add_popup

使用弹出窗口(...):

add_drag_payload

使用有效载荷(...):

add_drawlist

使用抽屉列表(...):

add_draw_layer

with draw_layer(...):

add_viewport_drawlist

with viewport_drawlist(...):

add_file_dialog

with file_dialog(...):

add_filter_set

with filter_set(...):

add_font

使用字体(...):

add_font_registry

with font_registry(...):

add_handler_registry

with handler_registry(...):

add_plot

使用Plot(...):

add_subplots

附子情节(...):

add_texture_registry

with texture_registry(...):

add_value_registry

with value_registry(...):

add_theme

主题(...):

add_item_pool

with item_pool(...):

add_template_registry

with template_registry(...):

福利:

  1. 自动将集装箱推送到集装箱堆栈。

  2. 自动将容器从容器堆栈中弹出。

  3. 更结构化、更具可读性的代码。

上下文管理器:

import dearpygui.dearpygui as dpg

dpg.create_context()

with dpg.window(label="Main"):

    with dpg.menu_bar():
        with dpg.menu(label="Themes"):
            dpg.add_menu_item(label="Dark")
            dpg.add_menu_item(label="Light")
            dpg.add_menu_item(label="Classic")

            with dpg.menu(label="Other Themes"):
                dpg.add_menu_item(label="Purple")
                dpg.add_menu_item(label="Gold")
                dpg.add_menu_item(label="Red")

        with dpg.menu(label="Tools"):
            dpg.add_menu_item(label="Show Logger")
            dpg.add_menu_item(label="Show About")

        with dpg.menu(label="Oddities"):
            dpg.add_button(label="A Button")
            dpg.add_simple_plot(label="Menu plot", default_value=(0.3, 0.9, 2.5, 8.9), height=80)

dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

显式父母分配(使用UUID):

import dearpygui.dearpygui as dpg

dpg.create_context()

w = dpg.add_window(label="Main")

mb = dpg.add_menu_bar(parent=w)

themes = dpg.add_menu(label="Themes", parent=mb)
dpg.add_menu_item(label="Dark", parent=themes)
dpg.add_menu_item(label="Light", parent=themes)

other_themes = dpg.add_menu(label="Other Themes", parent=themes)
dpg.add_menu_item(label="Purple", parent=other_themes)
dpg.add_menu_item(label="Gold", parent=other_themes)
dpg.add_menu_item(label="Red", parent=other_themes)

tools = dpg.add_menu(label="Tools", parent=mb)
dpg.add_menu_item(label="Show Logger", parent=tools)
dpg.add_menu_item(label="Show About", parent=tools)

oddities = dpg.add_menu(label="Oddities", parent=mb)
dpg.add_button(label="A Button", parent=oddities)
dpg.add_simple_plot(label="A menu plot", default_value=(0.3, 0.9, 2.5, 8.9), height=80, parent=oddities)

dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

明确的父母分配(使用别名):

import dearpygui.dearpygui as dpg

dpg.create_context()

dpg.add_window(label="Main", tag="w")

dpg.add_menu_bar(parent="w", tag="mb")

dpg.add_menu(label="Themes", parent="mb", tag="themes")
dpg.add_menu_item(label="Dark", parent="themes")
dpg.add_menu_item(label="Light", parent="themes")

dpg.add_menu(label="Other Themes", parent="themes", tag="other_themes")
dpg.add_menu_item(label="Purple", parent="other_themes")
dpg.add_menu_item(label="Gold", parent="other_themes")
dpg.add_menu_item(label="Red", parent="other_themes")

dpg.add_menu(label="Tools", parent="mb", tag="tools")
dpg.add_menu_item(label="Show Logger", parent="tools")
dpg.add_menu_item(label="Show About", parent="tools")

dpg.add_menu(label="Oddities", parent="mb", tag="Oddities")
dpg.add_button(label="A Button", parent="Oddities")
dpg.add_simple_plot(label="A menu plot", default_value=(0.3, 0.9, 2.5, 8.9), height=80, parent="Oddities")

dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

集装箱堆放作业:

import dearpygui.dearpygui as dpg

dpg.create_context()

dpg.push_container_stack(dpg.add_window(label="Main"))

dpg.push_container_stack(dpg.add_menu_bar())

dpg.push_container_stack(dpg.add_menu(label="Themes"))
dpg.add_menu_item(label="Dark")
dpg.add_menu_item(label="Light")
dpg.pop_container_stack()

dpg.push_container_stack(dpg.add_menu(label="Tools"))
dpg.add_menu_item(label="Show Logger")
dpg.add_menu_item(label="Show About")
dpg.pop_container_stack()

# remove menu_bar from container stack
dpg.pop_container_stack()

# remove window from container stack
dpg.pop_container_stack()

dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()