如何在 KiCAD pcbnew 插件中显示 wx 对话框消息

使用 KiCAD 的 pcbnew Python API 时,你可以使用以下代码片段显示对话框

show_wx_dialog_example.py
# 显示信息对话框
dlg = wx.MessageDialog(None, "Please select one or multiple footprints!\n...or use Ctrl+A to select everything.", "No footprints selected", wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()

注意你需要

import_wx.py
import wx

在插件顶部。

此代码将显示以下对话框:

KiCAD pcbnew 插件显示带错误图标的 wx MessageDialog,提示未选中封装

完整插件示例:

DialogExamplePlugin.py
#!/usr/bin/env python
import pcbnew
import wx

class DialogExamplePlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Show dialog example"
        self.category = "A descriptive category name"
        self.description = "A description of the plugin and what it does"
        self.show_toolbar_button = False # Optional, defaults to False

    def Run(self):
        dlg = wx.MessageDialog(None, "Please select one or multiple footprints!\n...or use Ctrl+A to select everything.", "No footprints selected", wx.OK | wx.ICON_ERROR)
        dlg.ShowModal()
        dlg.Destroy()

DialogExamplePlugin().register() # Instantiate and register to Pcbnew

你可以将此插件放在例如

kicad_plugin_path.txt
~/.local/share/kicad/7.0/scripting/plugins/DialogExamplePlugin.py

不要忘记从 pcbnew 菜单刷新插件


Check out similar posts by category: KiCad, Python