TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“ QLineEdit”

时间:2019-05-06 09:51:57

标签: python pyqt5

当我尝试将行编辑值转换为integer时收到错误消息。我的代码如下。我尝试将代码转换为int,但又收到另一条错误消息。所以我认为这种转换是必要的,但是如何?!

*错误消息*

Traceback (most recent call last):
  File "H:/Projects/PyQT/Chapter4/demoMultiLevelInheritance/callMultiLevelInheritance.py", line 58, in dispMessage
    int(self.ui.lineEditHistoryMarks.text()),int(self.ui.lineEditGeographyMarks))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'QLineEdit'

demoMultiLevelInheritance.py

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(435, 264)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(20, 20, 71, 16))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(20, 50, 71, 16))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(20, 80, 71, 16))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(20, 110, 91, 16))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(Dialog)
        self.label_5.setGeometry(QtCore.QRect(20, 140, 47, 13))
        self.label_5.setObjectName("label_5")
        self.label_6 = QtWidgets.QLabel(Dialog)
        self.label_6.setGeometry(QtCore.QRect(20, 170, 61, 16))
        self.label_6.setObjectName("label_6")
        self.lineEditCode = QtWidgets.QLineEdit(Dialog)
        self.lineEditCode.setGeometry(QtCore.QRect(140, 20, 191, 20))
        self.lineEditCode.setObjectName("lineEditCode")
        self.lineEditName = QtWidgets.QLineEdit(Dialog)
        self.lineEditName.setGeometry(QtCore.QRect(140, 50, 191, 20))
        self.lineEditName.setObjectName("lineEditName")
        self.lineEditHistoryMarks = QtWidgets.QLineEdit(Dialog)
        self.lineEditHistoryMarks.setGeometry(QtCore.QRect(140, 80, 191, 20))
        self.lineEditHistoryMarks.setObjectName("lineEditHistoryMarks")
        self.lineEditGeographyMarks = QtWidgets.QLineEdit(Dialog)
        self.lineEditGeographyMarks.setGeometry(QtCore.QRect(140, 110, 191, 20))
        self.lineEditGeographyMarks.setObjectName("lineEditGeographyMarks")
        self.lineEditTotal = QtWidgets.QLineEdit(Dialog)
        self.lineEditTotal.setEnabled(False)
        self.lineEditTotal.setGeometry(QtCore.QRect(140, 140, 191, 20))
        self.lineEditTotal.setObjectName("lineEditTotal")
        self.lineEditPercentage = QtWidgets.QLineEdit(Dialog)
        self.lineEditPercentage.setEnabled(False)
        self.lineEditPercentage.setGeometry(QtCore.QRect(140, 170, 191, 20))
        self.lineEditPercentage.setObjectName("lineEditPercentage")
        self.ButtonClickMe = QtWidgets.QPushButton(Dialog)
        self.ButtonClickMe.setGeometry(QtCore.QRect(144, 220, 141, 23))
        self.ButtonClickMe.setObjectName("ButtonClickMe")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Student Code"))
        self.label_2.setText(_translate("Dialog", "Student Name"))
        self.label_3.setText(_translate("Dialog", "History Marks"))
        self.label_4.setText(_translate("Dialog", "Geography Marks"))
        self.label_5.setText(_translate("Dialog", "Total"))
        self.label_6.setText(_translate("Dialog", "Percentage"))
        self.ButtonClickMe.setText(_translate("Dialog", "Click"))

callMultiLevelInheritance.py

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from Chapter4.demoMultiLevelInheritance.ui.demoMultiLevelInheritance import *

class Student:
    name =""
    code=""
    def __init__(self, name, code):
        self.name = name
        self.code = code

    def getName(self):
        return self.name

    def getCode(self):
        return self.code

class Marks(Student):
    historyMarks=""
    geoghraphyMarks=""

    def __init__(self,name, code, historyMarks, geoghraphyMarks):
        Student.__init__(self, name, code)
        self.historyMarks = historyMarks
        self.geoghraphyMarks = geoghraphyMarks

    def getHistoryMarks(self):
        return self.historyMarks

    def getGeoghraphyMarks(self):
        return self.geoghraphyMarks

class Result(Marks):
    totalMarks=""
    percantage=""

    def __init__(self,name, code, historyMarks, geoghraphyMarks ):
        Marks.__init__(self,name, code, historyMarks, geoghraphyMarks)
        self.totalMarks = historyMarks + geoghraphyMarks
        self.percantage = (historyMarks + geoghraphyMarks) / 200 *100

    def getTotalMarks(self):
        return self.totalMarks

    def getPercentage(self):
        return self.percantage

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.ButtonClickMe.clicked.connect(self.dispMessage)
        self.show()

    def dispMessage(self):
        resultObj = Result(self.ui.lineEditName.text(),self.ui.lineEditCode.text(),
                           int(self.ui.lineEditHistoryMarks.text()),int(self.ui.lineEditGeographyMarks))

        self.ui.lineEditTotal.setText(str(resultObj.getTotalMarks()))
        self.ui.lineEditPercentage.setText(str(resultObj.getPercentage()))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

 resultObj = Result(self.ui.lineEditName.text(),self.ui.lineEditCode.text(),
                           int(self.ui.lineEditHistoryMarks.text()),int(self.ui.lineEditGeographyMarks))

您正在将QLineEdit提供给int(int(self.ui.lineEditGeographyMarks))

您错过了:

.text()

int(self.ui.lineEditGeographyMarks.text())