我无法在一个函数中添加许多值

时间:2015-12-29 18:43:47

标签: python function instance

我有一个gui申请

  1. 我将文本放入文本框1,文本框2,............文本框70,然后单击pushButton

  2. 调用return_text ()中的函数module_b.py。现在我可以通过lambda1函数调用一个实例并在class_b中使用它,但是当我点击pushbutton时我无法调用70个实例。

  3. ** A-我想在lambda

    中将lineEdit_1,lineEdit_2 ...... lineEdit_70添加到main.py方法中

    ** B-我想在(return_text (self, txt))中修改(table2 (self, txt) )module_b.py以打印并返回值。 student1student70

    任何人都可以帮助我吗?这是代码:

    main.py

    # -*- coding: utf-8 -*-
    from PyQt4 import QtCore, QtGui
    import sys
    from GUI import Ui_MainWindow
    class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            from module_b import calss_b
            global instance_b
            instance_b=calss_b(self)
            txt1 = self.ui.lineEdit.text #Method reference not Method call
            txt2 = self.ui.lineEdit2.text 
            mySlot = lambda : (instance_b.return_text_username(txt1()) 
            QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL("clicked()"),mySlot)
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        global myapp
        myapp = MainWindow()
        myapp.show()
        sys.exit(app.exec_())
    

    module_b.py

    import sys
    from GUI import Ui_MainWindow
    from PyQt4 import QtCore, QtGui
    class calss_b (object):
         def __init__(self, parent=None):
          pass
    
    
         def return_text (self, txt):
      ####  global student1, student2 , student3………. Student70
            student1=unicode(txt) 
            return first_student
    #####   ….
            …
            return 70
    
        def table2 (self, txt):
            print student1
            print 2
    #####   ….
            …
            print 70
    

1 个答案:

答案 0 :(得分:1)

我能做的最好的方法是将所有lineEdit收集到一个列表中并将其传递给return_text方法,然后在每次迭代中调用text()方法,这样:< / p>

number_of_line_Edit = 70
txt = [getattr(self.ui,'lineEdit{0}'.format(i)) for i in range(1,number_of_line_Edit+1)] #This will collect all lineEdit(s) reference so we can call there methods in return_text method
mySlot = lambda :instance_b.return_text(txt)
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"),  mySlot) 

然后使用return_text的{​​{1}}和table2方法:

module_b.py

请注意,def return_text(self, lineEdit_list): my_text_list = [] for t in lineEdit_list: txt = unicode(t.text()) self.table2(txt) my_text_list.append(txt) #print my_text_list for checking purpose return my_text_list ## I want print password and return it. def table2(self, my_txt): print my_txt 列表将始终在每次调用my_text_list方法时重置为空列表,其中它将丢失之前调用的return_text个所有文本。