Python PyQt5:如何将 QPushButton 连接到插槽?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19966056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:06:16  来源:igfitidea点击:

PyQt5: How can I connect a QPushButton to a slot?

pythonqtsignals-slotspyqt5

提问by Ian Traum

Okay, so pretty much every tutorial/understandable-written-in-human-language-documentation is for PyQt4. But, PyQt5 changed how the whole 'connect button to a slot' works, and I still can't figure it out how to do it.

好的,几乎所有教程/可理解的人类语言文档都是针对 PyQt4 的。但是,PyQt5 改变了整个“将按钮连接到插槽”的工作方式,我仍然不知道该怎么做。

I did a quick gui in QtDesigner, and I have a QPushButton and a label. When I click the button, I want the text on the label to change. in C++ in QtDesigner, It's easy to connect the two. But I have to write it all in python.

我在 QtDesigner 中做了一个快速的 gui,我有一个 QPushButton 和一个标签。当我单击按钮时,我希望标签上的文本发生变化。在 QtDesigner 中的 C++ 中,很容易将两者连接起来。但是我必须全部用python编写。

I convert .ui file with pyuic5 to .py file. There, in Ui_MainWindow class, I can see setupUi method, that initializes self.button as follows

我使用 pyuic5 将 .ui 文件转换为 .py 文件。在那里,在 Ui_MainWindow 类中,我可以看到 setupUi 方法,它初始化 self.button 如下

self.testButton = QtWidgets.QPushButton(self.centralWidget)
self.testButton.setObjectName("newGame")

then, at the end of the method,

然后,在方法结束时,

QtCore.QMetaObject.connectSlotsByName(MainWindow)

is called, but, to be honest, I can't figure out what it does and what it connects to where.

被调用,但是,老实说,我无法弄清楚它的作用以及它连接到哪里。

in Main class, inheriting from QMainWindow, i write a following method

在从 QMainWindow 继承的 Main 类中,我编写了以下方法

@pyqtSlot(name='change')
def change_text(self):
    self.ui.testLabel.setText("Button Clicked!")

And i can't figure out how to connect the button signal to that slot. In pyqt4 I could set it up manually by doing button.clicked.connect(self.change_text), but as I've found out, PyQt5 obsoleted and discarded such simple set-up.

而且我不知道如何将按钮信号连接到该插槽。在 pyqt4 中,我可以通过执行 button.clicked.connect(self.change_text) 手动设置它,但正如我发现的那样,PyQt5 已经过时并丢弃了这种简单的设置。

Please, can anybody help me with that one?

拜托,有人可以帮我解决这个问题吗?

采纳答案by ekhumoro

I don't know where you got the idea that "PyQt5 changed how the whole 'connect button to a slot' works", but it is completely and utterly wrong. There have been no such changes, as can be readily seen from the official PyQt documentation:

我不知道你从哪里得到“PyQt5 改变了整个‘连接按钮到插槽’的工作方式”的想法,但这是完全错误的。从 PyQt 官方文档中可以很容易地看出,没有这样的变化:

But even without reading any documentation, it's easy enough to test for yourself. For example, in the following script, just switch comments on the first two lines, and it will run just the same:

但即使不阅读任何文档,自己进行测试也很容易。例如,在下面的脚本中,只需在前两行切换注释,它就会运行一样:

# from PyQt5.QtWidgets import (
from PyQt4.QtGui import (
    QApplication, QWidget, QVBoxLayout, QPushButton, QLabel,
    )

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.button = QPushButton('Test', self)
        self.label = QLabel(self)
        self.button.clicked.connect(self.handleButton)
        layout = QVBoxLayout(self)
        layout.addWidget(self.label)
        layout.addWidget(self.button)

    def handleButton(self):
        self.label.setText('Button Clicked!')

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

As for the other points: at your current state of knowledge, I would say you can safely ignore connectSlotsByNameand pyqtSlot. Although they have their uses (see the above docs for details), there's very rarely any real need to use them in 95% of applications.

至于其他要点:在您目前的知识状态下,我想说您可以放心地忽略connectSlotsByNamepyqtSlot。尽管它们有其用途(有关详细信息,请参阅上述文档),但很少有真正需要在 95% 的应用程序中使用它们。

For your specific case, the syntax is simply:

对于您的具体情况,语法很简单:

    self.testButton.clicked.connect(self.change_text)
    ...

def change_text(self):
    self.ui.testLabel.setText("Button Clicked!")