如何对按钮单击pyqt5做出反应 [英] How to react to a button click in pyqt5

查看:348
本文介绍了如何对按钮单击pyqt5做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是python编程的新手。我已经开始在pyqt5中实现UI,并且有一个按钮,我想在用户单击它时做出反应。

So, I am new to python programming. I have started to implement a UI in pyqt5 and there I have a button and I want to react when the user clicks it.

根据此链接我应该只写 btn.clicked.connect(self.buton_pressed)但是我收到消息在函数中找不到引用连接。 (周围的代码在问题的末尾)。

According to this Link I should simply write btn.clicked.connect(self.buton_pressed) however I get the message "Cannot find reference connect in function". (The surrounding code is at the end of the question)

所以我在Google上搜索了一下,发现所有内容只能以这种方式工作。我不明白为什么不这样做。我发现了这个 Stackoverflow问题,该问题还描述了如何执行此操作的旧方法。在进行了一些糊涂之后,我发现它在pyqt5或其他软件包中不再受支持。

So I googled a bit and all I found is that it should just work that way. I just don't get why it does not. I found this Stackoverflow question which also describes the old variant of how to do it. That did not work either, after some googeling I found out that it is no longer supported in pyqt5 or in some other package.

我尝试与该函数连接的函数event:

The function where I try to connec to the event:

def __add_button(self, text: str, layout: QLayout):
    btn = QPushButton(text, self)
    layout.addWidget(btn)
    btn.clicked.connect(self.button_pressed)
    # TODO: fix this.
    return btn

生成GUI的代码和调用的函数,在 __ init __ 函数

The code where the GUI is generated and the function called, in the __init__ function

lblhm = QLabel("Hauptmessung", self)
layout.addWidget(lblhm)

self.__hm_b = self.__add_button("Messung öffnen", layout)
self.__hm_config_b = self.__add_button("Configruation öffnen", layout)

lblzm = QLabel("Zusatzmessung", self)
layout.addWidget(lblzm)

self.__zm_b = self.__add_button("Messung öffnen", layout)
self.__zm_config_b = self.__add_button("Configuration öffnen", layout)

The button_pressed函数尚未实现,但是应该打开一个openFile对话框来选择文件。

The button_pressed function is not yet implemented, but it is supposed to open a openFile dialog for file selection.

根据这篇文章我可以在返回函数后直接连接,但随后我必须写它4次不是很好。

According to this post i could just connect after returning the function, but then i would have to write it 4 times which is not very nice. Isn't the signal bound to the object not to the variable?

感谢您的帮助:)

推荐答案

由于您没有向我们提供工作示例,因此很难理解您的问题,即,一个可以使代码按原样运行的和平方式。像这样:

It's hard to understand your problem since you don't provide us with a working example, i.e. a peace of code one can run "as is". Something like this:

from PyQt4 import QtCore, QtGui

class MyWindow(QtGui.QWidget):
    def __init__(self):
        super().__init__()

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)

        lblhm = QtGui.QLabel("Hauptmessung", self)
        layout.addWidget(lblhm)

        self.__hm_b = self.__add_button("Messung öffnen", layout)
        self.__hm_config_b = self.__add_button("Configruation öffnen", layout)

        lblzm = QtGui.QLabel("Zusatzmessung", self)
        layout.addWidget(lblzm)

        self.__zm_b = self.__add_button("Messung öffnen", layout)
        self.__zm_config_b = self.__add_button("Configuration öffnen", layout)

    def button_pressed(self):
        print('Button pressed')

    def __add_button(self, text: str, layout: QtGui.QLayout):
        btn = QtGui.QPushButton(text, self)
        layout.addWidget(btn)
        btn.clicked.connect(self.button_pressed)
        return btn



if __name__== '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    wnd = MyWindow()
    wnd.show()
    sys.exit(app.exec_())

PyQt4 。它与 PyQt5 一起使用吗?

这篇关于如何对按钮单击pyqt5做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆