[英]PyQt5: The connected slot to a clicked signal not working
first of all, sorry for my English.首先,对不起我的英语。 My mother tongue is Spanish.
我的母语是西班牙语。 So, I am working on this mini project to learn python.
所以,我正在做这个迷你项目来学习 python。 I'm learning how to do UI using PyQt5.
我正在学习如何使用 PyQt5 做 UI。 This application is simple, it has three inputs, one button, and one output.
这个应用程序很简单,它有三个输入、一个按钮和一个 output。 I'm using MVC software pattern in this app and I have my view, model, and controller in separate files.
我在这个应用程序中使用 MVC 软件模式,我在单独的文件中有我的观点,model 和 controller。
THE PROBLEM: In the controller class I connect the only button I have to a slot called (_calculate).问题:在 controller class 中,我将唯一的按钮连接到一个名为 (_calculate) 的插槽。 When I run the app and press that button the terminal should print a text so I can see if it is working.
当我运行应用程序并按下该按钮时,终端应该打印一个文本,以便我可以查看它是否正常工作。 The terminal shows me nothing.
终端什么也没显示。 Traying different sorts of things I discovered that if I do the same binding in the view class, _calculate is executed.
处理不同类型的事情我发现如果我在视图 class 中执行相同的绑定,_calculate 将被执行。 I did a tutorial about a Calculator using PyQt5.
我做了一个关于使用 PyQt5 的计算器的教程。 The calculator from the tutorial work just fine using MVC, so I used that to find out if I forget or miss something but nothing obvious appear.
教程中的计算器使用 MVC 工作得很好,所以我用它来确定我是否忘记或错过了一些东西,但没有明显的出现。
My controller class我的 controller class
class Controller:
def __init__(self, view):
self._view = view
self._connectSignals()
def _connectSignals(self):
self._view.button.clicked.connect(self._calculate)
def _calculate(self):
print('trying to calculate')
My view class我的看法 class
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QSpacerItem
from PyQt5.QtWidgets import QSizePolicy
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import TextEdit
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
from ToolController import Controller
class UserInterface(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('BDO Tool')
self.setFixedSize(450, 300)
self._centralWidget = QWidget(self)
self.setCentralWidget(self._centralWidget)
self._createWindowSkeleton()
def _createWindowSkeleton(self):
# Vertical container who contains all the program widget
self._generalLayout = QVBoxLayout()
self._centralWidget.setLayout(self._generalLayout)
self._generalLayout.setAlignment(Qt.AlignCenter)
self._generalLayout.addLayout(self._createFirstRow())
self._generalLayout.addLayout(self._createButton())
self._generalLayout.addWidget(self._createAreaText())
def _createFirstRow(self):
hLayout = QHBoxLayout()
spacer = QSpacerItem(20, 20, hPolicy=QSizePolicy.Expanding)
self._inputBoxes = {
self.INPUT_BASE_FAIL: (QPixmap(self.IMG_BASE_FAILS), QLineEdit()),
self.INPUT_TARGET_FAIL: (QPixmap(self.IMG_TARGET_FAIL), QLineEdit()),
self.INPUT_STACK_AMOUNT: (QPixmap(self.IMG_STACK_AMOUNT), QLineEdit()),
}
keys = list(self._inputBoxes.keys())
for key, value in self._inputBoxes.items():
pixmap, editLine = value
label = QLabel()
label.setPixmap(pixmap)
editLine.setFixedWidth(40)
editLine.setAlignment(Qt.AlignRight)
hLayout.addWidget(label)
hLayout.addWidget(editLine)
if key != keys[-1]:
hLayout.addSpacerItem(spacer)
return hLayout
def _createButton(self):
self.button = QPushButton('Calculate')
spacer = QSpacerItem(20, 20, hPolicy=QSizePolicy.Expanding)
hLayout = QHBoxLayout()
hLayout.addSpacerItem(spacer)
hLayout.addWidget(self.button)
hLayout.addSpacerItem(spacer)
return hLayout
def _createAreaText(self):
self._infoDisplay = QTextEdit()
self._infoDisplay.setEnabled(False)
return self._infoDisplay
INPUT_BASE_FAIL = 1
INPUT_TARGET_FAIL = 2
INPUT_STACK_AMOUNT = 3
IMG_BASE_FAILS = 'img\\user25x25.png'
IMG_TARGET_FAIL = 'img\\target25x25.png'
IMG_STACK_AMOUNT = 'img\\stack25x25.png'
My main我的主要
import sys
from PyQt5.QtWidgets import QApplication
from ToolView import UserInterface
from ToolController import Controller
def main():
app = QApplication(sys.argv)
view = UserInterface()
view.show()
Controller(view=view)
sys.exit(app.exec())
if __name__ == '__main__':
main()
The problem is that you didn't create a persistent object for the controller instance, so the instance is immediately garbage collected afterwards because it isn't referenced anywhere else.问题是您没有为 controller 实例创建持久的 object,因此该实例随后会立即被垃圾收集,因为它没有在其他任何地方引用。
As long as a reference to the instance exists, it will work as expected.只要对实例的引用存在,它就会按预期工作。
In this case a local variable will suffice, since app.exec()
will block further processing within main
, ensuring that the instance will exist until it exists.在这种情况下,一个局部变量就足够了,因为
app.exec()
将阻止main
中的进一步处理,确保实例在它存在之前一直存在。
def main():
app = QApplication(sys.argv)
view = UserInterface()
view.show()
controller = Controller(view=view)
sys.exit(app.exec())
Nonetheless, let me tell you that while conceptually using an MVC is usually a good idea, you should use it carefully, without "exaggerating" the pattern and only if it really helps the development.尽管如此,让我告诉您,虽然在概念上使用 MVC 通常是一个好主意,但您应该谨慎使用它,不要“夸大”模式,并且只有当它确实有助于开发时。
I'd like to point up one of the disadvantages of MVC :我想指出MVC 的缺点之一:
Lack of incremental benefit – UI applications are already factored into components, and achieving code reuse and independence via the component architecture, leaving no incremental benefit to MVC.
缺乏增量收益——UI 应用程序已经被分解为组件,并通过组件架构实现代码重用和独立性,没有给 MVC 留下增量收益。
I understand that yours is a simple and conceptual example, but it certainly is an overly complicated one for what it does.我知道你的例子是一个简单的概念性例子,但它确实是一个过于复杂的例子。 For instance:
例如:
UserInterface
class there are 4 functions that will most certainly be used only once.UserInterface
class 中,有 4 个函数肯定只会使用一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.