2017-03-16 34 views
0

main_window是由設計者和pyuic生成的.py文件。如何在PyQt5中將文本從一個類文件從另一個類文件中獲取?

 
※ 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() 但是這個代碼返回空值

幫助我..請..

# -*- 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_()) 

回答

0

您在eng_mgmt必須win_Main一個參考,因爲你已經將它設置爲父項。只需調用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

相關問題