如何在PyQt5中从一个类到另一个类获取lienedit文本?

时间:2017-03-16 07:57:51

标签: pyqt

main_window是由designer和pyuic生成的.py文件。

※ SCENARIO
1. program start
2. input ip(lienedit)/port(lineedit_2)/id(lineedit_3)/password(lineedit_4)
3. click pushButton_2 (called self.pushButton_2.clicked.connect(self.engine_mgmt))
4. popup eng_mgmt_window
5. click auth_Start_Btn (called self.auth_Start_Btn.clicked.connect(self.auth_func))
6. popup ip text (to get lineedit.text())... but this code is null Popup... T.T

我想叫做lineedit.text() 但此代码返回值为null

帮帮我..请..

# -*- coding:utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
import os, sys, time, re, paramiko
from Main_Window import main_window
from Engine_Mgmt import engine_mgmt_window

class win_Main(QtWidgets.QDialog, main_window):
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent)
        self.setupUi(self)

        self.pushButton.clicked.connect(self.param_check)
        self.pushButton_2.clicked.connect(self.engine_mgmt)

    def param_check(self):
        conn_ip = self.lineEdit.text()
        conn_port = self.lineEdit_2.text()
        user_id = self.lineEdit_4.text()
        user_pw = self.lineEdit_5.text()

        if conn_ip == "" or conn_port == "" or user_id == "" or user_pw == "":
            QtWidgets.QMessageBox.information(self, "Param Check Error", "Input filed cannot be left blank")

    def engine_mgmt(self):
        self.dialog = eng_mgmt_window(self)
        self.dialog.show()


class eng_mgmt_window(engine_mgmt_window, win_Main):
    def __init__(self, parent=win_Main):
        QtWidgets.QDialog.__init__(self, parent)
        self.setupUi(self)        
        self.auth_Start_Btn.clicked.connect(self.auth_func)

    def auth_func(self):
        # 
        # INPUT win_Main() in lineedit = 192.168.0.1 but Result... Null
        # I WANT GET win_Main() in lineedit text
        #
        gets.QMessageBox.information(self, "Config Check", wm.lineEdit.text())


if __name__ == "__main__":
    import sys    

    app = QtWidgets.QApplication(sys.argv)
    dlg = win_Main()
    dlg.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

您在win_Main中引用了eng_mgmt,因为您已将其设置为父级。只需调用self.parent()即可。

这里有MCVE您的代码,管理窗口正确访问主窗口GUI中的值。

from PyQt5 import QtCore, QtGui, QtWidgets

class win_Main(QtWidgets.QDialog):
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent)
        self.layout = QtWidgets.QVBoxLayout()
        self.pushButton_2 = QtWidgets.QPushButton("Button")
        self.lineEdit = QtWidgets.QLineEdit()
        self.layout.addWidget(self.pushButton_2)
        self.layout.addWidget(self.lineEdit)
        self.pushButton_2.clicked.connect(self.engine_mgmt)
        self.setLayout(self.layout)

    def engine_mgmt(self):
        self.dialog = eng_mgmt_window(self)
        self.dialog.show()


class eng_mgmt_window(QtWidgets.QDialog):
    def __init__(self, parent=win_Main):
        QtWidgets.QDialog.__init__(self, parent)
        self.auth_Start_Btn = QtWidgets.QPushButton("Now push me", parent=self)
        self.auth_Start_Btn.clicked.connect(self.auth_func)

    def auth_func(self):
        # print(self.parent().lineEdit.text())
        msg = QtWidgets.QMessageBox()
        msg.setText(self.parent().lineEdit.text())
        msg.exec_()

if __name__ == "__main__":
    import sys    

    app = QtWidgets.QApplication(sys.argv)
    dlg = win_Main()
    dlg.show()
    sys.exit(app.exec_())

注意:eng_mgmt_window没有理由继承win_Main