如何从主类调用GUI对象或更多对象

时间:2015-12-28 19:29:57

标签: python lambda pyqt4 call instance

我有一个gui申请

  1. 我将文字放入文本框1,文本框2,然后点击pushButton
  2. 调用return_text_username ()中的函数moduel_b.py。 现在我可以通过lambda1函数调用一个实例并在class_b中使用它,但是当我点击pushbutton时我无法调用两个时刻。
  3. ** A-我想在lineEdit_2中的lambda方法中添加main.py或在instance_lambda2_password方法中添加connect

    ** B-我想在return_printtext_password (self, txt)中编辑moduel_b.py以打印并返回密码。

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

    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)
            instance_lambda1_username=lambda: instance_b.return_text_username(self.ui.lineEdit.text())##I want to add lineEdit_2 
            instance_lambda2_password=lambda: instance_b.printtext2(self.ui.lineEdit_2.text())
            QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), \
            instance_lambda1_username)## Or add instance_lambda2_password here.
    
    
    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_username (self, txt):
            username=unicode(txt) 
            print username  
            return username 
    
          ## I want print password and return it.   
         def return_printtext_password (self, txt):
            password=unicode(txt) 
            print password   
            return password  
    

1 个答案:

答案 0 :(得分:0)

您不需要为每个lambda定义slot函数,而是将它们组合成一个并将其传递给connect方法,这样:

txt1 = self.ui.lineEdit.text #Method reference not Method call
txt2 = self.ui.lineEdit2.text 
mySlot = lambda : (instance_b.return_text_username(txt1()), instance_b.return_printtext_password(txt2()))# Passed to lambda method call this time
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"),  mySlot)

对于Point-B,我认为你已经设法做到了,

 def return_text_username (self, txt):
     username=unicode(txt) 
     print username  
     return username