python - 无法获得信号以连接到 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()
解决方案
推荐阅读
- python - 基于正则表达式标记推文
- android - Android 样式未正确覆盖
- ios - 如何使用 Swift 模拟按键?
- electron - Electron 的 printtopdf() 可以用应用程序的名称将生成的文档标记为“内容创建者”吗?
- mysql - 从逗号分隔字符串mysql的值中获取位置
- dotnetbrowser - 如何修复在 winform 应用程序中加载 dotnetbrowser 实例时不需要的焦点切换
- c - 错误:需要左值作为赋值的左操作数(swrots)
- javascript - 使用 AudioWorklet 流式传输捕获的音频时,如何防止中断/断断续续/故障?
- python - 检索 Matplotlib 热图颜色
- reactjs - 将 js 图像反应为 base64