Python QT - 刷新所有小部件

时间:2017-02-08 22:27:45

标签: python qt

我有一个简单的python QT乘法程序。有3个字段,第一个随机数(只读),第二个随机数(只读)和第三行编辑,供用户输入结果。如果first * second = result,则显示Right Label,否则显示Wrong Label。我想用再次播放按钮替换退出按钮,所以当用户按下它时,重新生成随机数并清除所有结果标签,用户再试一次。

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(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(650, 644)
        self.label_71 = QtGui.QLabel(Form)
        self.label_71.setGeometry(QtCore.QRect(70, 20, 481, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Andika"))
        font.setPointSize(26)
        self.label_71.setFont(font)
        self.label_71.setObjectName(_fromUtf8("label_71"))
        self.layoutWidget = QtGui.QWidget(Form)
        self.layoutWidget.setGeometry(QtCore.QRect(200, 570, 221, 29))
        self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
        self.horizontalLayout_51 = QtGui.QHBoxLayout(self.layoutWidget)
        self.horizontalLayout_51.setMargin(0)
        self.horizontalLayout_51.setObjectName(_fromUtf8("horizontalLayout_51"))
        self.pushButton_2 = QtGui.QPushButton(self.layoutWidget)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_2.clicked.connect(self.exit_)
        self.horizontalLayout_51.addWidget(self.pushButton_2)
        self.pushButton = QtGui.QPushButton(self.layoutWidget)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton.clicked.connect(self.praxis)
        self.horizontalLayout_51.addWidget(self.pushButton)

        ###  Line Edit 1 - 2 ###
        self.count = 124
        self.list_x1 = []
        self.list_y1 = []
        for i in range(10):
            self.x1 = randint(2,9)
            self.list_x1.append(self.x1)
            self.ln1 = "self.lineEdit1_" + str(i)
            self.ln1 = QtGui.QLineEdit(Form)
            self.ln1.setText(str(self.x1))
            self.ln1.setReadOnly(True)
            self.ln1.setGeometry(QtCore.QRect(74, self.count, 40, 26))
            self.ln1.setAlignment(QtCore.Qt.AlignCenter)
            self.ln1.setObjectName(_fromUtf8("lineEdit"))

            self.y1 = randint(2,9)
            self.list_y1.append(self.y1)
            self.ln2 = "self.lineEdit2_" + str(i)
            self.ln2 = QtGui.QLineEdit(Form)
            self.ln2.setText(str(self.y1))
            self.ln2.setReadOnly(True)
            self.ln2.setGeometry(QtCore.QRect(140, self.count, 40, 26))
            self.ln2.setAlignment(QtCore.Qt.AlignCenter)
            self.ln2.setObjectName(_fromUtf8("lineEdit_2"))

            self.count += 35

        #Εισαγωγή 1ης στήλης    
        self.count = 124
        self.ln3_value = []
        for i in range(10):
            self.ln3 = "self.lineEdit3_" + str(i)
            self.ln3_value.append(self.ln3)
            self.ln3_value[i] = QtGui.QLineEdit(Form)
            self.ln3_value[i].setGeometry(QtCore.QRect(206, self.count, 40, 26))
            self.ln3_value[i].setAlignment(QtCore.Qt.AlignCenter)
            self.ln3_value[i].setObjectName(_fromUtf8("lineEdit_3"))
            self.count += 35

        ###  Line Edit 3- 4 ###
        self.count = 124
        self.list_x2 = []
        self.list_y2 = []
        for i in range(10):
            self.x2 = randint(2,9)
            self.list_x2.append(self.x2)
            self.ln4 = "self.lineEdit4_" + str(i)
            self.ln4 = QtGui.QLineEdit(Form)
            self.ln4.setText(str(self.x2))
            self.ln4.setReadOnly(True)
            self.ln4.setGeometry(QtCore.QRect(348, self.count, 40, 26))
            self.ln4.setAlignment(QtCore.Qt.AlignCenter)
            self.ln4.setObjectName(_fromUtf8("lineEdit_4"))

            self.y2 = randint(2,9)
            self.list_y2.append(self.y2)
            self.ln5 = "self.lineEdit5_" + str(i)
            self.ln5 = QtGui.QLineEdit(Form)
            self.ln5.setText(str(self.y2))
            self.ln5.setReadOnly(True)
            self.ln5.setGeometry(QtCore.QRect(414, self.count, 40, 26))
            self.ln5.setAlignment(QtCore.Qt.AlignCenter)
            self.ln5.setObjectName(_fromUtf8("lineEdit_5"))
            self.count += 35

        #Εισαγωγή 2ης στήλης
        self.count = 124
        self.ln6_value = []
        for i in range(10):
            self.ln6 = "self.lineEdit5_" + str(i)
            self.ln6_value.append(self.ln3)
            self.ln6_value[i] = QtGui.QLineEdit(Form)
            self.ln6_value[i].setGeometry(QtCore.QRect(480, self.count, 40, 26))
            self.ln6_value[i].setAlignment(QtCore.Qt.AlignCenter)
            self.ln6_value[i].setObjectName(_fromUtf8("lineEdit_6"))
            self.count += 35
        ###   / Line Edit 1 - 4 ###


        ###   Labels 1 - 6 ###
        self.lbl1_count = 119
        for i in range(10):
            self.lbl_1 = "self.label1_" + str(i)
            self.lbl_1 = QtGui.QLabel(Form)
            self.lbl_1.setGeometry(QtCore.QRect(120, self.lbl1_count, 14, 34))
            self.lbl_1.setText(_translate("Form", "X", None))
            font = QtGui.QFont()
            font.setFamily(_fromUtf8("Andika"))
            font.setPointSize(16)
            self.lbl_1.setFont(font)
            self.lbl_1.setObjectName(_fromUtf8("label"))
            self.lbl1_count += 35

        self.lbl2_count = 119
        for i in range(10):
            self.lbl_2 = "self.label2_" + str(i)
            self.lbl_2 = QtGui.QLabel(Form)
            self.lbl_2.setGeometry(QtCore.QRect(186, self.lbl2_count, 14, 34))
            self.lbl_2.setText(_translate("Form", "=", None))
            font = QtGui.QFont()
            font.setFamily(_fromUtf8("Andika"))
            font.setPointSize(16)
            self.lbl_2.setFont(font)
            self.lbl_2.setObjectName(_fromUtf8("label_2"))
            self.lbl2_count += 35

        self.lbl3_list = []
        self.lbl3_count = 119
        for i in range(10):
            self.lbl_3 = "self.label3_" + str(i)
            self.lbl3_list.append(self.lbl_3)
            self.lbl3_list[i] = QtGui.QLabel(Form)
            self.lbl3_list[i].setGeometry(QtCore.QRect(252, self.lbl3_count, 80, 34))
            self.lbl3_list[i].setText(_translate("Form", "?", None))
            font = QtGui.QFont()
            font.setFamily(_fromUtf8("Andika"))
            font.setPointSize(14)
            self.lbl3_list[i].setFont(font)
            self.lbl3_list[i].setObjectName(_fromUtf8("label_3"))
            self.lbl3_count += 35

        self.lbl4_count = 119
        for i in range(10):
            self.lbl_4 = "self.label2_" + str(i)
            self.lbl_4 = QtGui.QLabel(Form)
            self.lbl_4.setGeometry(QtCore.QRect(394, self.lbl4_count, 14, 34))
            self.lbl_4.setText(_translate("Form", "X", None))
            font = QtGui.QFont()
            font.setFamily(_fromUtf8("Andika"))
            font.setPointSize(16)
            self.lbl_4.setFont(font)
            self.lbl_4.setObjectName(_fromUtf8("label_2"))
            self.lbl4_count += 35

        self.lbl5_count = 119
        for i in range(10):
            self.lbl_5 = "self.label5_" + str(i)
            self.lbl_5 = QtGui.QLabel(Form)
            self.lbl_5.setGeometry(QtCore.QRect(460, self.lbl5_count, 14, 34))
            self.lbl_5.setText(_translate("Form", "=", None))
            font = QtGui.QFont()
            font.setFamily(_fromUtf8("Andika"))
            font.setPointSize(16)
            self.lbl_5.setFont(font)
            self.lbl_5.setObjectName(_fromUtf8("label_5"))
            self.lbl5_count += 35

        self.lbl6_list = []
        self.lbl6_count = 119
        for i in range(10):
            self.lbl_6 = "self.label6_" + str(i)
            self.lbl6_list.append(self.lbl_6)
            self.lbl6_list[i] = QtGui.QLabel(Form)
            self.lbl6_list[i].setGeometry(QtCore.QRect(526, self.lbl6_count, 80, 34))
            self.lbl6_list[i].setText(_translate("Form", "?", None))
            font = QtGui.QFont()
            font.setFamily(_fromUtf8("Andika"))
            font.setPointSize(14)
            self.lbl6_list[i].setFont(font)
            self.lbl6_list[i].setObjectName(_fromUtf8("label_6"))
            self.lbl6_count += 35

        ###   / Labels 1 - 6 ###

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

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Multiplication", None))
        self.label_71.setText(_translate("Form", "Multiplication", None))
        self.pushButton_2.setText(_translate("Form", "Exit", None))
        self.pushButton.setText(_translate("Form", "Check", None))

    def praxis(self):
        self.lbl3_count = 119
        for i in range(10):
            self.val = self.ln3_value[i]
            self.val2 = self.ln6_value[i]

            self.get_val = self.val.text()
            self.get_val2 = self.val2.text()

            self.get_val_str = str(self.get_val)
            self.get_val2_str = str(self.get_val2)


            self.summary = self.list_y1[i] * self.list_x1[i]
            self.summary2 = self.list_y2[i] * self.list_x2[i]

            self.summary_int = str(self.summary)
            self.summary_int2 = str(self.summary2)

            if self.summary_int == self.get_val_str:
                self.lbl3_list[i].setText(_translate("Form", "Σωστό!", None))
                self.lbl3_list[i].setStyleSheet('color: green')

            else:
                self.lbl3_list[i].setText(_translate("Form", "Λάθος!", None))
                self.lbl3_list[i].setStyleSheet('color: red')

            if self.summary_int2 == self.get_val2_str:
                self.lbl6_list[i].setText(_translate("Form", "Σωστό!", None))
                self.lbl6_list[i].setStyleSheet('color: green')

            else:
                self.lbl6_list[i].setText(_translate("Form", "Λάθος!", None))
                self.lbl6_list[i].setStyleSheet('color: red')

    def exit_(self):
        import sys
        sys.exit()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

0 个答案:

没有答案