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

时间:2018-01-02 00:23:58

标签: python python-2.7 pyqt pyqt4

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

  1. 如果结果正确则打印“正确!结果为......”并将背景颜色更改为绿色。
  2. 如果结果错误则打印“...”并将背景颜色更改为红色
  3. enter image description here

    现在,问题是:我在连接到“新问题”按钮的函数中生成随机数。如果我点击“检查结果”,那么这些数字将不会传递给“检查结果”按钮。没有类定义我通常通过返回传递值,但是,这里使用类self它不起作用。

    • 问题1:解决此问题的最佳方法是什么?
    • 问题2:我不知道如何更改这些文本字段的颜色。

    非常感谢任何帮助!

    到目前为止我得到的代码是:

    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()
    

1 个答案:

答案 0 :(得分:1)

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

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)

然后将这种对象创建为窗口小部件的属性并处理逻辑,因为它是可以在类的整个范围内访问的属性。

要更改颜色,我使用小部件的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)