简介有用链接GithubDocsWiki Example安装安装很简单可以直接使用pip install dearpygui安装。源码编译安装如果想从源码编译安装可以使用如下命令sudoaptinstalllibxrandr-dev libxinerama-dev libxcursor-dev libxi-devgitclone--recursivehttps://github.com/hoffstadt/DearPyGuicdDearPyGui pipinstall.# orpython setup.pyinstall多线程编译安装将setup.py中的--config Release修改为--config Release -- -j$(nproc)即可实现cmake与make的多线程编译。如下所示修改完成后执行如下代码编译安装pipinstall.# 或者python setup.pyinstall源码分析参数传递set_positional_configuration输入Python 管道输出C结构体Python顺序传参给C位置参数set_configuration输入Python字典输出C配置Python改配置同步给C关键字参数fill_configuration_dict输入C配置输出Python字典用于导出配置给Python只读GetEntityParser(mvAppItemType type)入口参数解析以heat sseries为例首先看Python的绘制函数add_heat_seriesdefadd_heat_series(x:Union[List[float],Tuple[float,...]],rows:int,cols:int,*,label:strNone,user_data:AnyNone,use_internal_label:boolTrue,tag:Union[int,str]0,parent:Union[int,str]0,before:Union[int,str]0,source:Union[int,str]0,show:boolTrue,scale_min:float0.0,scale_max:float1.0,bounds_min:Any(0.0,0.0),bounds_max:Any(1.0,1.0),format:str%0.1f,contribute_to_bounds:boolTrue,col_major:boolFalse,**kwargs)-Union[int,str]: Adds a heat series to a plot. Args: x (Any): rows (int): cols (int): label (str, optional): Overrides name as label. user_data (Any, optional): User data for callbacks use_internal_label (bool, optional): Use generated internal label instead of user specified (appends ### uuid). tag (Union[int, str], optional): Unique id used to programmatically refer to the item.If label is unused this will be the label. parent (Union[int, str], optional): Parent to add this item to. (runtime adding) before (Union[int, str], optional): This item will be displayed before the specified item in the parent. source (Union[int, str], optional): Overrides id as value storage key. show (bool, optional): Attempt to render widget. scale_min (float, optional): Sets the color scale min. Typically paired with the color scale widget scale_min. scale_max (float, optional): Sets the color scale max. Typically paired with the color scale widget scale_max. bounds_min (Any, optional): bounds_max (Any, optional): format (str, optional): contribute_to_bounds (bool, optional): col_major (bool, optional): data will be read in column major order id (Union[int, str], optional): (deprecated) Returns: Union[int, str] ifidinkwargs.keys():warnings.warn(id keyword renamed to tag,DeprecationWarning,2)tagkwargs[id]returninternal_dpg.add_heat_series(x,rows,cols,labellabel,user_datauser_data,use_internal_labeluse_internal_label,tagtag,parentparent,beforebefore,sourcesource,showshow,scale_minscale_min,scale_maxscale_max,bounds_minbounds_min,bounds_maxbounds_max,formatformat,contribute_to_boundscontribute_to_bounds,col_majorcol_major,**kwargs)对应C中实际的绘制函数是draw_heat_series具体定义在在mvPlotting.h里structmvHeatSeriesConfig:_mvBasicSeriesConfig{introws1;intcols1;doublescale_min0.0;doublescale_max1.0;std::string format%0.1f;ImPlotPoint bounds_min{0.0,0.0};ImPlotPoint bounds_max{1.0,1.0};ImPlotHeatmapFlags flagsImPlotHeatmapFlags_None;};casemvAppItemType::mvHeatSeries:returnadd_heat_series;classmvHeatSeries:publicmvAppItem{public:mvHeatSeriesConfig configData{};explicitmvHeatSeries(mvUUID uuid):mvAppItem(uuid){}voidhandleSpecificPositionalArgs(PyObject*dict)override{DearPyGui::set_positional_configuration(dict,configData);}voiddraw(ImDrawList*drawlist,floatx,floaty)override{DearPyGui::draw_heat_series(drawlist,*this,configData);}voidhandleSpecificKeywordArgs(PyObject*dict)override{DearPyGui::set_configuration(dict,configData);}voidgetSpecificConfiguration(PyObject*dict)override{DearPyGui::fill_configuration_dict(configData,dict);}voidsetDataSource(mvUUID dataSource)override{DearPyGui::set_data_source(*this,dataSource,configData.value);}void*getValue()override{returnconfigData.value;}PyObject*getPyValue()override{returnToPyList(*configData.value);}voidsetPyValue(PyObject*value)override{*configData.valueToVectVectDouble(value);}};具体实现在mvPlotting.cpp里voidDearPyGui::draw_heat_series(ImDrawList*drawlist,mvAppItemitem,constmvHeatSeriesConfigconfig)用于参数传递的三个函数如下// Python -- C, 位置参数voidDearPyGui::set_positional_configuration(PyObject*inDict,mvHeatSeriesConfigoutConfig){if(!VerifyRequiredArguments(GetParsers()[GetEntityCommand(mvAppItemType::mvHeatSeries)],inDict))return;(*outConfig.value)[0]ToDoubleVect(PyTuple_GetItem(inDict,0));outConfig.rowsToInt(PyTuple_GetItem(inDict,1));outConfig.colsToInt(PyTuple_GetItem(inDict,2));(*outConfig.value)[1].push_back(outConfig.bounds_min.y);(*outConfig.value)[1].push_back(outConfig.bounds_max.y);}// Python -- C, 关键字参数voidDearPyGui::set_configuration(PyObject*inDict,mvHeatSeriesConfigoutConfig){if(inDictnullptr)return;if(PyObject*itemPyDict_GetItemString(inDict,format))outConfig.formatToString(item);if(PyObject*itemPyDict_GetItemString(inDict,rows))outConfig.rowsToInt(item);if(PyObject*itemPyDict_GetItemString(inDict,cols))outConfig.colsToInt(item);if(PyObject*itemPyDict_GetItemString(inDict,bounds_min))outConfig.bounds_minToPoint(item);if(PyObject*itemPyDict_GetItemString(inDict,bounds_max))outConfig.bounds_maxToPoint(item);if(PyObject*itemPyDict_GetItemString(inDict,scale_min))outConfig.scale_minToDouble(item);if(PyObject*itemPyDict_GetItemString(inDict,scale_max))outConfig.scale_maxToDouble(item);// helper for bit flippingautoflagop[inDict](constchar*keyword,intflag,intflags){if(PyObject*itemPyDict_GetItemString(inDict,keyword))ToBool(item)?flags|flag:flags~flag;};// flagsflagop(col_major,ImPlotHeatmapFlags_ColMajor,outConfig.flags);boolvalueChangedfalse;if(PyObject*itemPyDict_GetItemString(inDict,x)){valueChangedtrue;(*outConfig.value)[0]ToDoubleVect(item);}if(valueChanged){(*outConfig.value)[1].push_back(outConfig.bounds_min.y);(*outConfig.value)[1].push_back(outConfig.bounds_max.y);}}// C -- Python关键字参数voidDearPyGui::fill_configuration_dict(constmvHeatSeriesConfiginConfig,PyObject*outDict){if(outDictnullptr)return;PyDict_SetItemString(outDict,format,mvPyObject(ToPyString(inConfig.format)));PyDict_SetItemString(outDict,rows,mvPyObject(ToPyInt(inConfig.rows)));PyDict_SetItemString(outDict,cols,mvPyObject(ToPyInt(inConfig.cols)));PyDict_SetItemString(outDict,bounds_min,mvPyObject(ToPyPair(inConfig.bounds_min.x,inConfig.bounds_min.y)));PyDict_SetItemString(outDict,bounds_max,mvPyObject(ToPyPair(inConfig.bounds_max.x,inConfig.bounds_max.y)));PyDict_SetItemString(outDict,scale_min,mvPyObject(ToPyDouble(inConfig.scale_min)));PyDict_SetItemString(outDict,scale_max,mvPyObject(ToPyDouble(inConfig.scale_max)));// helper to check and set bitautocheckbitset[outDict](constchar*keyword,intflag,constintflags){mvPyObject py_resultToPyBool(flagsflag);PyDict_SetItemString(outDict,keyword,py_result);};// flagscheckbitset(col_major,ImPlotHeatmapFlags_ColMajor,inConfig.flags);}在mvAppItem.cpp文件里有入口参数解析实现mvPythonParserDearPyGui::GetEntityParser(mvAppItemType type)比如下面是HeatSeries的casemvAppItemType::mvHeatSeries:{AddCommonArgs(args,(CommonParserArgs)(MV_PARSER_ARG_ID|MV_PARSER_ARG_PARENT|MV_PARSER_ARG_BEFORE|MV_PARSER_ARG_SOURCE|MV_PARSER_ARG_SHOW));args.push_back({mvPyDataType::DoubleList,x});args.push_back({mvPyDataType::Integer,rows});args.push_back({mvPyDataType::Integer,cols});args.push_back({mvPyDataType::Double,scale_min,mvArgType::KEYWORD_ARG,0.0,Sets the color scale min. Typically paired with the color scale widget scale_min.});args.push_back({mvPyDataType::Double,scale_max,mvArgType::KEYWORD_ARG,1.0,Sets the color scale max. Typically paired with the color scale widget scale_max.});args.push_back({mvPyDataType::DoubleList,bounds_min,mvArgType::KEYWORD_ARG,(0.0, 0.0)});args.push_back({mvPyDataType::DoubleList,bounds_max,mvArgType::KEYWORD_ARG,(1.0, 1.0)});args.push_back({mvPyDataType::String,format,mvArgType::KEYWORD_ARG,%0.1f});args.push_back({mvPyDataType::Bool,contribute_to_bounds,mvArgType::KEYWORD_ARG,True});args.push_back({mvPyDataType::Bool,col_major,mvArgType::KEYWORD_ARG,False,data will be read in column major order});setup.aboutAdds a heat series to a plot.;setup.category{Plotting,Containers,Widgets};break;}绘图热图使用热图功能可以得到 类似matlab的imagesc和Matplotlib的imshow的效果。具体函数为add_heat_series其中format参数用于控制每个矩阵元素的数值显示格式需设为空字符使用bounds_min和bounds_max设置坐标轴刻度范围。其它参数含义参见文档 heat3D绘图支持集成Implot3D需要注意的是imgui是docking版本 可以在release页面 通过tags来查看各个版本imgui主页 https://github.com/ocornut/imguiimplot3d主页 https://github.com/brenocq/implot3d/implot主页 https://github.com/epezent/implot将implot3d放在thirdparty文件夹下然后修改DearPyGui/src下的CMakeList.txt文件添加如下内容设置源文件和头文件set(MARVEL_SOURCES# implot3d../thirdparty/implot3d/implot3d.cpp../thirdparty/implot3d/implot3d_items.cpp../thirdparty/implot3d/implot3d_meshes.cpp../thirdparty/implot3d/implot3d_demo.cppset(MARVEL_INCLUDE_DIR.../thirdparty/implot../thirdparty/implot3d集成自带demo然后按如下各图集成implot3d自带的demo下面两处必须添加否则会报段错误或直接退出。修改完代码后编译安装修改的库然后运行如下示例代码importdearpygui.dearpyguiasdpgimportdearpygui.demoasdemo dpg.create_context()dpg.create_viewport(titleCustom Title,width600,height600)demo.show_demo()dpg.show_implot_demo()dpg.show_implot3d_demo()dpg.setup_dearpygui()dpg.show_viewport()dpg.start_dearpygui()dpg.destroy_context()运行后弹出如下窗口可查看示例效果疑难杂症卡顿发现如果窗口数多于1个界面操作起来就会卡顿而如果注释掉dpg.configure_app的代码就不卡顿了。怀疑是参数配置不当但改了无果。最终发现是dpg.configure_app和create_viewport调用的位置不对。参照官方文档init file更改代码后正常。importdearpygui.dearpyguiasdpg dpg.create_context()defsave_init():dpg.save_init_file(dpg.ini)dpg.configure_app(init_filedpg.ini)# default file is dpg.iniwithdpg.window(labelabout,tagmain window):dpg.add_button(labelSave Window pos,callbacklambda:save_init)withdpg.window(labelabout,tagside window):dpg.add_button(labelPress me)dpg.create_viewport(titleCustom Title,width800,height600)dpg.setup_dearpygui()dpg.show_viewport()dpg.start_dearpygui()dpg.destroy_context()