如何将额外参数传递给 PyQt GUI 函数?

How do I pass extra arguments to PyQt GUI functions?

我正在尝试使用一些图形小部件为我的孩子构建一个简单的 1x1 练习工具。如下图所示,"New Problem" 按钮在内部生成 2 个随机数并将其显示在文本字段中。用户必须在 = 旁边提供结果。对于 "Check Result",我想要一条条件指令,使:

  1. 如果结果正确打印 "Correct! Result is ..." 并将背景颜色更改为绿色。
  2. 如果结果错误打印“...”并将背景颜色更改为红色

现在,问题是:我在连接到 "New Problem" 按钮的函数中生成随机数。如果我点击 "Check Result",这些数字将不会传递给 "check result" 按钮。如果没有 class 定义,我通常通过 return 传递值,但是,这里使用 class self 它不起作用。

非常感谢任何帮助!

目前我得到的代码是:

from PyQt4 import QtGui
import sys
import random
import numpy as np
import new_design6  # translated from Qt designer (not relevant for this question)

class ExampleApp(QtGui.QMainWindow,new_design6.Ui_MainWindow):


    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.exit_app.clicked.connect(self.exit_application)
        self.get_new_problem.clicked.connect(self.generate_new_problem)
        self.check_result.clicked.connect(self.check_problem_result)


    def generate_new_problem(self):
        # clear each field 
        self.show_problem.clear()
        self.input_result.clear()
        self.show_result.clear()

        u = int(np.random.randint(0,11,1))
        v = int(np.random.randint(0,11,1))
        w = str(u) + " x " + str(v)

        self.show_problem.setReadOnly(True)
        self.show_problem.setPlainText(w)
        # how to pass my random numbers ?
        return(u,v) #*---> Problem line1* 


    def check_problem_result(self,u,v): #*---> Problem line1* 
        input_result_number=self.input_result.toPlainText()
        result=int(input_result_number)

        # here I'd like to have a conditional question to check if result is correkt or wrong
        if (result == u*v):
            result_string="Correct! Result is:  " + str(result)
            self.show_result.setPlainText(result_string)
        else: 
            result_string="Wrong! Result is:  " + str(result) + "Try another one"


    def exit_application(self):
        self.close()

def main():
    app = QtGui.QApplication(sys.argv)
    form = ExampleApp()
    form.show()
    app.exec_()

if __name__=='__main__':
    main()

一个合适的方法是创建一个 class 来管理操作:

class Operation:
    def __init__(self):
        self.params = []

    def setParams(self, params):
        self.params = params

    def process(self):
        # processing
        u, v = self.params
        result = u*v
        return result 

    def toString(self):
        return "{}x{}".format(*self.params)

然后创建一个此类对象作为小部件的属性并处理逻辑,因为它是一个 属性,可以在 class 的整个范围内访问。

要更改颜色,我使用小部件的 QPalette,如以下代码所示:

class ExampleApp(QtGui.QMainWindow,new_design6.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.exit_app.clicked.connect(self.close)
        self.get_new_problem.clicked.connect(self.generate_new_problem)
        self.check_result.clicked.connect(self.check_problem_result)
        self.operation = Operation()
        self.show_problem.setReadOnly(True)

    def generate_new_problem(self):
        self.show_problem.clear()
        self.input_result.clear()
        self.show_result.clear()
        pal = self.show_result.palette()
        pal.setColor(QtGui.QPalette.Base, QtCore.Qt.white)
        self.show_result.setPalette(pal)
        u = int(np.random.randint(0,11,1))
        v = int(np.random.randint(0,11,1))
        params = u, v
        self.operation.setParams(params)        
        self.show_problem.setPlainText(self.operation.toString())

    def check_problem_result(self):
        input_result_number = self.input_result.toPlainText()
        result = int(input_result_number)
        pal = self.show_result.palette()
        if self.operation.process() == result:
            result_string="Correct! Result is:  {}".format(result)
            pal.setColor(QtGui.QPalette.Base, QtCore.Qt.green)
        else: 
            result_string="Wrong! Result is: {} Try another one".format(result)
            pal.setColor(QtGui.QPalette.Base, QtCore.Qt.red)
        self.show_result.setPlainText(result_string)
        self.show_result.setPalette(pal)