如何从主类调用GUI对象或更多对象 [英] How do I call an GUI object or more from main class

查看:82
本文介绍了如何从主类调用GUI对象或更多对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gui应用程序

I have a gui application


  1. 我将文本放入文本框1,文本框2,然后单击 pushButton

  2. moduel_b.py中的函数 return_text_username() 被调用。
    现在我可以通过 lambda1 函数调用一个实例,并在 class_b 中使用它,但是我无法调用我单击按钮的两个瞬间。

  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想要在 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屋!

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