首页 > 解决方案 > 无法获得信号以连接到 PyQT5 中的插槽

问题描述

这是我的第一篇文章,大家好!所以我正在尝试学习 Python 3.8 和 PyQT5,并且我正在使用 Spyder。我一直在学习有关 MVC 编程的教程,并决定用控制器编写我自己的非常基本的 GUI(还没有接触到模型!)。然而,我完全不明白为什么我不能使用连接命令在控制器类中运行一个函数(我什至在教程中看到了这一点,但无法弄清楚我做错了什么)。

完整代码如下。

在 Ctrl 类中,函数 _connectSignals(self)应该连接信号和插槽,但是:

self._view.hi.clicked.connect(self._calculateResult)

不调用 _calculateResult 函数(单击按钮时没有任何反应,但是如果我将其更改为:

self._view.hi.clicked.connect(self._view.setDisplayText)

它工作正常,按下按钮时调用该函数。

为什么我不能在 ctrl 类中调用 _calculateResult 函数并使用它来调用 _view.setDisplayText?

对此的任何帮助将不胜感激!

谢谢

马特

import sys

# Import QApplication and the required widgets from PyQt5.QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget

# Create a subclass of QMainWindow to setup the GUI
class PyUi(QMainWindow):
    """View (GUI)."""

    def __init__(self):
        """View initializer."""
        super().__init__()
        # Set some main window's properties
        self.setWindowTitle("copy")
        self.setFixedSize(100,100)
        # Set the central widget and the general layout
        self.generalLayout = QVBoxLayout()
        self._centralWidget = QWidget(self)
        # self._centralWidget = QPushButton('copy')
        self.setCentralWidget(self._centralWidget)
        self._centralWidget.setLayout(self.generalLayout)
        # # Create the display and the buttons
        self._createDisplay()

    def _createDisplay(self):
        """Create the display."""
        # # Create button
        self.hi = QPushButton('Hi')
        # # Set output
        self.write = QLineEdit()
        self.write.setFixedHeight(35)
        self.write.setAlignment(Qt.AlignRight)
        self.write.setReadOnly(True)
 
        # Add the display to the general layout
        self.generalLayout.addWidget(self.hi)
        self.generalLayout.addWidget(self.write)
        
    def setDisplayText(self):
        """Set display's text."""

        self.write.setText('Hello to you too')
    
# Create a Controller class
class Ctrl:
    """Controller."""

    def __init__(self, view):
        """Controller initializer."""
        self._view = view
        # Connect signals and slots
        self._connectSignals()

    def _calculateResult(self):
        """Evaluate expressions."""
        self._view.setDisplayText()

    def _connectSignals(self):

        self._view.hi.clicked.connect(self._calculateResult) # doesnt work
       # self._view.hi.clicked.connect(self._view.setDisplayText) # works

# Client code
def main():
    """Main function."""
    # Create an instance of `QApplication`
    py = QApplication(sys.argv)
    # Show GUI
    view = PyUi()
    view.show()
    # Create instances of the model and the controller
    Ctrl(view=view)
    # Execute main loop
    sys.exit(py.exec_())

if __name__ == "__main__":
    main()

标签: pythonqtpyqtpyqt5

解决方案


推荐阅读