如何从主类调用GUI对象或更多对象 [英] How do I call an GUI object or more from main class
问题描述
我有一个gui应用程序
I have a gui application
- 我将文本放入文本框1,文本框2,然后单击
pushButton
, -
moduel_b.py中的函数
被调用。return_text_username()
现在我可以通过lambda1
函数调用一个实例,并在class_b
中使用它,但是我无法调用我单击按钮
的两个瞬间。
- I put text into text box1, text box2,and then click on the
pushButton
, - The function
return_text_username ()
in themoduel_b.py
be called. Now I can call one instance bylambda1
function and use it inclass_b
, but I can not call two instants when I click on thepushbutton
.
** A- I想要在 main.py
的lambda方法中添加 lineEdit_2
或添加 instance_lambda2_password
进入 connect
方法。
**A- I want add lineEdit_2
into lambda method in main.py
or add instance_lambda2_password
into connect
method.
** B-我想编辑 return_printtext_password( self,txt)
在 moduel_b.py
中进行打印并返回密码。
**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
推荐答案
您不需要为每个<$ c定义 lambda
函数$ c>插槽,而是将它们分组为一个,然后通过以下方式将其传递到您的 connect
方法:
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)
至于B点,我认为您已经设法
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
这篇关于如何从主类调用GUI对象或更多对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!