I can not add many values in one function

淺唱寂寞╮ 提交于 2019-12-12 03:34:53

问题


I have a gui application

  1. I put text into text box1, text box2,………… text box70 ,and then click on the pushButton,

  2. The function return_text () in the module_b.py be called. Now I can call one instance by lambda1 function and use it in class_b, but I can not call 70 instances when I click on the pushbutton.

**A- I want add lineEdit_1 , lineEdit_2 ….. lineEdit_70 into lambda method in main.py

**B- I want to edit (return_text (self, txt)) and (table2 (self, txt) ) in the module_b.py to print and return values from . student1 to student70

Can anyone help me? Here's the code for that :

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:


The best way I can thing of, is to collect all the lineEdit into a list and pass it to return_text method then call text() method in each iteration, this way:

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) 

Then in return_text and table2 methods of 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

Note that my_text_list list will be always reset to empty list every time return_text method is called, where it will lose all texts of lineEdit(s) of previous call.



来源:https://stackoverflow.com/questions/34517084/i-can-not-add-many-values-in-one-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!