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

问题描述 投票:0回答:1

仅“ plugin2.py”文件有效。忽略“ plugin.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"}]
}

python python-3.x pyqt pyqt5
1个回答
1
投票

您的代码无法正常工作,因为您需要保留对所有这些对象的引用,因为您在self.x上进行了设置,因此先前的引用已丢失。

我已经更改了项目的结构,然后可以使其在我的本地环境中运行。

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

main.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_())

plugin.py

from PyQt5.QtCore import QObject


class Window(QObject):

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

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

plugin2.py

from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla2")
© www.soinside.com 2019 - 2024. All rights reserved.