我不能在一个函数中添加许多值 [英] I can not add many values in one function

查看:77
本文介绍了我不能在一个函数中添加许多值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gui应用程序


  1. 我把文本放入文本框1,文本框2,............文本框70,然后点击 pushButton


  2. 函数 return_text() module_b.py 中被调用。现在我可以通过 lambda1 函数调用一个实例,并在 class_b 中使用它,但是当我点击按钮


** A-我在 main.py

中添加lineEdit_1,lineEdit_2 ... .. lineEdit_70到 lambda 中的方法

** B-我想编辑(return_text(self,txt))(table2(self,txt) ) module_b.py 中打印并返回值。 student1 student70



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

main.py

 # -  *  - 编码:utf-8  -  *  -  
从PyQt4导入QtCore,QtGui
导入sys $ b $从GUI导入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#方法引用不是方法调用
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)
全局myapp
myapp = MainWindow()
myapp.show()
sys.ex它(app.exec_())

module_b.py
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b class calss_b(object):
def __init __(self,parent = None):
pass

$ b def return_text(self,txt):
####全球student1,student2,student3 .........。 Student70
student1 = unicode(txt)
返回first_student
##### ...。
...
返回70

def table2(self,txt):
print student1
print 2
##### ...。
...
print 70


解决方案

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

  number_of_line_Edit (1,number_of_line_Edit + 1)]#这将收集所有的lineEdit(s)引用我们可以在return_text方法中调用方法
mySlot = lambda:instance_b.return_text(txt)
QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL(clicked()),mySlot )

然后在 return_text 和<$ c

  def return_text(self,lineEdit_list):
my_text_list = []
for lineEdit_list:
txt = unicode(t.text())
self.tabl e2(txt)
my_text_list.append(txt)
#print my_text_list用于检查目的
返回my_text_list


##我要打印密码并返回它。
def table2(self,my_txt):
print my_txt

请注意<每次调用 return_text 方法时,code> my_text_list 列表将始终重置为空列表,其中将丢失<$ $的所有文本c $ c> lineEdit (s)前次调用。


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

解决方案

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.

这篇关于我不能在一个函数中添加许多值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆