问题
I have a gui application
I put text into text box1, text box2,………… text box70 ,and then click on the
pushButton
,The function
return_text ()
in themodule_b.py
be called. Now I can call one instance bylambda1
function and use it inclass_b
, but I can not call 70 instances when I click on thepushbutton
.
**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