문제

Qt 4.8.6을 사용하여 Qtdesigner (Python)를 배우기 시작했습니다.이 튜토리얼을 따르십시오.

https://www.youtube.com/watch?v=glqrzliiw2e <./ P>

그러나 제목에서 때때로 오류를 보여주는 것은 attributeError : 'ui_form'객체는 'printham_btn'속성이 없습니다.누군가가해야 할 일을 말해 주거나 내 코드를 수정해야합니다.

감사합니다!

이 문제 가이 포럼에 이미 게시되었음을 알고 있지만 내 경우에해야 할 일을 알아낼 수 없습니다.

코드 :

from PyQt4 import QtCore, QtGui
import sys

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.verticalLayout_2 = QtGui.QVBoxLayout(Form)
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.print_ham = QtGui.QPushButton(Form)
        self.print_ham.setObjectName(_fromUtf8("print_ham"))
        self.verticalLayout.addWidget(self.print_ham)
        self.verticalLayout_2.addLayout(self.verticalLayout)

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

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Super ham", None))
        self.print_ham.setText(_translate("Form", "print ham", None))
        self.printHam_btn.clicked.connect(self.printHam)

    def printHam(self):
        print('Ham!')

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_window = Ui_Form()
    main_window.show()
    sys.exit(app.exec_())
.

도움이 되었습니까?

해결책

문제는 다음과 같습니다.

self.printHam_btn.clicked.connect(self.printHam)
.

QpushButton 인스턴스를 다르게 호출 하므로이 행을 변경해야합니다.

self.print_ham.clicked.connect(self.printHam)
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top