将textChanged函数动态连接到添加的类| PyQt5

2024-05-23 13:56:44 发布

您现在位置:Python中文网/ 问答频道 /正文

只有“plugin2.py”文件在工作。“已忽略”插件.py““

entry刚刚连接上一个指定的“x”值

我希望我的所有插件都连接到textChanged函数。我该怎么办?你知道吗

※如果将self.x更改为x不工作,但我没有得到任何错误。你知道吗

※如果删除self.x变量和类型:

self.entry.textChanged.connect((__import__(plug["name"]).Window().textChangedd))

结果是一样的,不工作,但没有错误

pluginSystem/
    main.py
    plugin.py
    plugin2.py
    package.json

main.py

#imports

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.vBox = QVBoxLayout()
        self.entry = QLineEdit()
        self.vBox.addWidget(self.entry)

        with open("package.json") as f:
            data = json.load(f)

        for plug in data["Plugin"]:
            importlib.import_module(plug["name"])
            self.x = (__import__(plug["name"]).Window().textChangedd)
            self.entry.textChanged.connect(self.x)

        self.entry.textChanged.connect(self.textChanged)

        self.setLayout(self.vBox)
        self.show()

    def textChanged(self, text):
        if text == "close":
            app.quit()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())

plugin.py

from pluginSystem.main import *

class Window(QObject):

    def __init__(self):
        super().__init__()

    @pyqtSlot(str)
    def textChangedd(self, text):
        print("blabla")

plugin2.py

#Same as Plugin.py

package.json

{
  "Plugin": [{"name" : "plugin"},{"name" : "plugin2"}]
}


Tags: namepyimportselfjsoninitmaindef
1条回答
网友
1楼 · 发布于 2024-05-23 13:56:44

您的代码无法工作,因为您需要保留对所有这些对象的引用,当您在self.x上设置时,前面的引用将丢失。你知道吗

我已经改变了你项目的结构,然后我可以让它在我当地的环境中工作。你知道吗

plugin_system/
    main.py
    plugins/
        plugin.py
        plugin2.py
    package.json

主.py

from PyQt5.QtWidgets import QWidget, QApplication, QLineEdit, QVBoxLayout
import sys
import importlib
import json

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.vBox = QVBoxLayout()
        self.entry = QLineEdit()
        self.vBox.addWidget(self.entry)
        self.plugins = []

        with open("package.json") as f:
            data = json.load(f)

        for plug in data["Plugin"]:
            plugin_module = importlib.import_module(
                "plugins.{}".format(plug["name"])
            )
            plugin_object = plugin_module.Window()
            self.entry.textChanged.connect(plugin_object.textChangedd)

            #Keeping reference to all of the plugin objects
            self.plugins.append(plugin_object)

        self.entry.textChanged.connect(self.textChanged)

        self.setLayout(self.vBox)
        self.show()

    def textChanged(self, text):
        if text == "close":
            app.quit()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())

插件.py

from PyQt5.QtCore import QObject


class Window(QObject):

    def __init__(self, parent=None):
        super().__init__(parent)

    def textChangedd(self, text):
        print("blabla1")

插件2.py

from PyQt5.QtCore import QObject


class Window(QObject):

    def __init__(self, parent=None):
        super().__init__(parent)

    def textChangedd(self, text):
        print("blabla2")

相关问题 更多 >