我有一个gui申请
pushButton
,return_text_username ()
中的函数moduel_b.py
。
现在我可以通过lambda1
函数调用一个实例并在class_b
中使用它,但是当我点击pushbutton
时我无法调用两个时刻。 ** 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
答案 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