分期¶
分级系统用于创建项或项层次结构,其中父/根将在稍后时间确定。
暂存项目不会提交以进行渲染。
暂存的项目将显示在项目注册表中。
通过使用可以将项目移出暂存 move_item
。
最基本的例子如下:
import dearpygui.dearpygui as dpg
dpg.create_context()
def stage_items():
with dpg.stage(tag="stage1"):
dpg.add_text("hello, i was added from a stage", tag="text_tag")
def present_stage_items():
dpg.move_item("text_tag", parent="main_win")
with dpg.window(label="Tutorial", tag="main_win"):
dpg.add_button(label="stage items", callback=stage_items)
dpg.add_button(label="present stages items", callback=present_stage_items)
dpg.show_item_registry()
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
“取消上台”物品的首选方法是 unstage
。这将根据的标准规则放置项目,就像它们是新创建的项目一样 集装箱堆叠 。
此外,使用UnStage命令将自动清理舞台容器。
使用 push_container_stack
和 pop_container_stack
在此推荐使用,因为它在取消转移时提供了更好的性能。
import dearpygui.dearpygui as dpg
dpg.create_context()
def stage_items():
with dpg.stage(tag="stage1"):
dpg.add_text("hello, i was added from a stage")
dpg.add_text("hello, i was added from a stage")
dpg.add_text("hello, i was added from a stage")
dpg.add_text("hello, i was added from a stage")
dpg.add_text("hello, i was added from a stage")
def present_stage_items():
dpg.push_container_stack("main_win")
dpg.unstage("stage1")
dpg.pop_container_stack()
with dpg.window(label="Tutorial", tag="main_win", height=400, width=400):
dpg.add_button(label="stage items", callback=stage_items)
dpg.add_button(label="present stages items", callback=present_stage_items)
dpg.show_item_registry()
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
使用类包装项¶
该系统最明显的好处是对于将DPG项包装到类中的高级用户。而不必将各种可配置选项复制为类的成员(以避免调用 configure_item
或 get_item_configuration
在实际创建项之前),您可以在包装类的构造函数中创建并暂存项!
以下是两个示例:
示例1
import dearpygui.dearpygui as dpg
dpg.create_context()
class Button:
def __init__(self, label):
with dpg.stage() as self._staging_container_id:
self._id = dpg.add_button(label=label)
def set_callback(self, callback):
dpg.set_item_callback(self._id, callback)
def get_label(self):
return dpg.get_item_label(self._id)
def submit(self, parent):
dpg.push_container_stack(parent)
dpg.unstage(self._staging_container_id)
dpg.pop_container_stack()
my_button = Button("Press me")
my_button.set_callback(lambda: print("I've been pressed!"))
print(my_button.get_label())
with dpg.window(label="Tutorial", tag="main_win"):
dpg.add_text("hello world")
my_button.submit("main_win")
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
示例2
import dearpygui.dearpygui as dpg
dpg.create_context()
class Window:
def __init__(self, label):
self._children = []
with dpg.stage() as stage:
self.id = dpg.add_window(label=label)
self.stage = stage
def add_child(self, child):
dpg.move_item(child.id, parent=self.id)
def submit(self):
dpg.unstage(self.stage)
class Button:
def __init__(self, label):
with dpg.stage():
self.id = dpg.add_button(label=label)
def set_callback(self, callback):
dpg.set_item_callback(self.id, callback)
my_button = Button("Press me")
my_button.set_callback(lambda: print("I've been pressed!"))
my_window = Window("Tutorial")
my_window.add_child(my_button)
my_window.submit()
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()