Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将textChanged函数动态连接到添加的类| PyQt5_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

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

Python 将textChanged函数动态连接到添加的类| PyQt5,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,只有“plugin2.py”文件在工作。忽略“plugin.py” 条目刚刚连接的最后一个指定的“x”值 我希望我的所有插件都连接到textChanged函数。我该怎么办 ※如果将self.x更改为x不工作,但我没有收到任何错误 ※如果删除self.x变量和类型: self.entry.textChanged.connect((__import__(plug["name"]).Window().textChangedd)) 结果是一样的,不工作,但没有错误 pluginSystem/

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

条目
刚刚连接的最后一个指定的“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

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_())
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla1")
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla2")
#导入
类窗口(QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.vBox=QVBoxLayout()
self.entry=QLineEdit()
self.vBox.addWidget(self.entry)
将open(“package.json”)作为f:
data=json.load(f)
对于插件数据[“插件”]:
importlib.import_模块(插件[“名称”])
self.x=(uuu导入_uuu(插件[“名称]).Window().textChangedd)
self.entry.textChanged.connect(self.x)
self.entry.textChanged.connect(self.textChanged)
self.setLayout(self.vBox)
self.show()
def textChanged(自我,文本):
如果文本==“关闭”:
app.quit()
如果名称=“\uuuuu main\uuuuuuuu”:
app=QApplication(sys.argv)
window=window()
sys.exit(app.exec())
plugin.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_())
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla1")
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla2")
从pluginSystem.main导入*
类窗口(QObject):
定义初始化(自):
super()。\uuuu init\uuuuu()
@pyqtSlot(str)
def textChangedd(自我,文本):
打印(“blabla”)
plugin2.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_())
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla1")
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla2")
#与Plugin.py相同
package.json

{
“Plugin”:[{“name”:“Plugin”},{“name”:“plugin2”}]
}

您的代码不起作用,因为您需要保留对所有这些对象的引用,当您在
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_())
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla1")
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla2")
plugin.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_())
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla1")
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla2")
plugin2.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_())
from PyQt5.QtCore import QObject


class Window(QObject):

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

    def textChangedd(self, text):
        print("blabla1")
from PyQt5.QtCore import QObject


class Window(QObject):

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

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