简体   繁体   中英

I cannot reach to QLineEdit.text() value from another python file

I have 2 python file: 1 of them was generated by QtDesigner(main.py) and another one I normally store my functions(a.py). I am trying to reach to the value of QLineEdit.text() value from a.py but I cannot.

main.py

from PyQt5 import QtCore, QtGui, QtWidgets
from a import *

class Ui_Dialog(object):

    def returnvalue(self):           //my code
       return self.lineEdit.text()   //my code


    def setupUi(self, Dialog):
       Dialog.setObjectName("Dialog")
       Dialog.resize(641, 517)
       self.lineEdit = QtWidgets.QLineEdit(Dialog)
       self.lineEdit.setGeometry(QtCore.QRect(250, 110, 113, 22))
       self.lineEdit.setObjectName("lineEdit")
       self.pushButton = QtWidgets.QPushButton(Dialog)
       self.pushButton.setGeometry(QtCore.QRect(260, 160, 93, 28))
       self.pushButton.setObjectName("pushButton")

       self.retranslateUi(Dialog)
       QtCore.QMetaObject.connectSlotsByName(Dialog)
       self.pushButton.clicked.connect(p.clickme)
   def retranslateUi(self, Dialog):
       _translate = QtCore.QCoreApplication.translate
       Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
       self.pushButton.setText(_translate("Dialog", "PushButton"))


if __name__ == "__main__":
   import sys
   app = QtWidgets.QApplication(sys.argv)
   Dialog = QtWidgets.QDialog()
   ui = Ui_Dialog()
   ui.setupUi(Dialog)
   Dialog.show()
   sys.exit(app.exec_())enter code here

a.py:

from main import *

class p():
    def clickme(self):
       print(Ui_Dialog.returnvalue(self))

p = p()

Error message:

Traceback (most recent call last):
File "c:\Users\husey\Desktop\Fiver\test\a.py", line 6, in clickme
   print(Ui_Dialog.returnvalue(self))
File "c:\Users\husey\Desktop\Fiver\test\main.py", line 6, in returnvalue
   return self.lineEdit.text()
AttributeError: 'p' object has no attribute 'lineEdit'

Pass in the arguments using lambda function.

self.pushButton.clicked.connect(lambda: p.clickme(self.lineEdit.text()))

a.py:

class p():
    def clickme(txt_from_gui):
        print(txt_from_gui)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM