How do I call an GUI object or more from main clas

2019-08-14 05:07发布

问题:

I have a gui application

  1. I put text into text box1, text box2,and then click on the pushButton,
  2. The function return_text_username () in the moduel_b.py be called. Now I can call one instance by lambda1 function and use it in class_b, but I can not call two instants when I click on the pushbutton.

**A- I want add lineEdit_2 into lambda method in main.py or add instance_lambda2_password into connect method.

**B- I want to edit return_printtext_password (self, txt) in the moduel_b.py to print and return password.

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)
        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:

You don't need to define lambda function for each slot, instead group them into one and pass it to your connect method, this way:

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)

As for Point-B, I think you've already managed to do it,

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