簡體   English   中英

將 textChanged 函數動態連接到添加的類 | PyQt5

[英]Connect textChanged function to a added class dynamically | PyQt5

只有“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

主文件

#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())

插件.py

from pluginSystem.main import *

class Window(QObject):

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

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

插件2.py

#Same as Plugin.py

包.json

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

您的代碼不起作用,因為您需要保留對所有這些對象的引用,因為您正在設置self.x ,之前的引用正在丟失。

我已經改變了你的項目的結構,然后我可以讓它在我的本地環境中工作。

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

主文件

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM