1

I use the pyqt5.I connect the pushbutton signal to the dididi(). It is supported when I clicked the button it will print the message, but when I click the button, it does not print the message. Why? What can I do to solve it?

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'bank1.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QBasicTimer
from PyQt5.QtGui import QColor, QFontMetrics, QPainter, QPalette
from PyQt5.QtWidgets import (QApplication, QDialog, QLineEdit, QVBoxLayout,
        QWidget)
from PyQt5.QtCore import QCoreApplication

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(80, 200, 71, 21))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(230, 200, 72, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.textEdit = QtWidgets.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(140, 90, 104, 31))
        self.textEdit.setObjectName("textEdit")
        self.pushButton.clicked.connect(self.dididi)
        self.pushButton_2.clicked.connect(self.dididi)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)            

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "RCT"))
        self.pushButton.setText(_translate("Form", "连ζŽ₯"))
        self.pushButton_2.setText(_translate("Form", "发送"))
    def dididi(self):
        print("hello world")

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    widget = QWidget(None)
    Ui_Form().setupUi(widget)
    widget.show()
    sys.exit(app.exec_())
    pass
Filippo Mazza
  • 4,339
  • 4
  • 22
  • 25
user6821315
  • 11
  • 1
  • 3

2 Answers2

0

Just put @staticmethod above your "dididi()" method.

See explanation about @staticmethod @classmethod and normal methods.

@staticmethod
def dididi():
    print("hello world")
Community
  • 1
  • 1
yurisnm
  • 1,630
  • 13
  • 29
0

If you don't want to loose the context (the self) then you could do it this way:

Instead of directly connecting the clicked event to your method, in tour setupUi you create an internal function and you connect the clicked event to it. This would do the trick as you still have access to the self object at this context:

 def clicked():
     self.dididi()
 self.pushButton.clicked.connect(clicked)

This way, you can still define your dididi function as a normal method and use local variables of your class via the self argument.

Dr ALOUI
  • 331
  • 4
  • 8