Answer the question
In order to leave comments, you need to log in
Sharing data between pyqt forms?
Help to figure out how to transfer data from one form to another.
The first form on pressing of the button causes the second. In the second form, enter the data in the lineEdit field, and by pressing the button, the second form closes and all the data from lineedit is displayed on the main form in the lineedit field.
main.py
import sys
from gui import *
from gui2 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.funk)
def funk(self):
MyApp2.show()
class SecondWin(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.second = Ui_Form()
self.second.setupUi(self)
self.second.pushButton.clicked.connect(self.funk2)
def funk2(self):
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Myapp = MainWin()
MyApp2 = SecondWin()
Myapp.show()
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt5 UI code generator 5.5
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(488, 352)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(30, 20, 421, 51))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 90, 251, 28))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 488, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Вызвать форму"))
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gui2.ui'
#
# Created by: PyQt5 UI code generator 5.5
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(60, 130, 281, 28))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 50, 351, 51))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(80, 10, 291, 31))
self.label.setStyleSheet("font: 14pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Отобразить в главной форме"))
self.label.setText(_translate("Form", "Тут пишем текст"))
Answer the question
In order to leave comments, you need to log in
Explain how to share data between forms??
Why is the aboutshow()self.pushButton.clicked.connect(self.aboutshow)
function called on click, it seems to work correctly
def aboutshow(self):
strq = self.lineEdit.text()
self.MyWin = StartMyMain()
self.MyWin.obr(strq)
print(strq)
self.close()
def obr(self,strs):
if strs:
self.ui.lineEdit.setText(strs)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import time
from gui import *
from gui2 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class AboutUs(QtWidgets.QMainWindow, Ui_Form):
def __init__(self,parent=None):
super().__init__(parent, QtCore.Qt.Window)
self.setupUi(self)
self.pushButton.clicked.connect(self.aboutshow)
def aboutshow(self):
strq = self.lineEdit.text()
self.MyWin = StartMyMain()
self.MyWin.obr(strq)
print(strq)
self.close()
class StartMyMain(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.AboutUs = None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.uiAbout = AboutUs()
self.ui.pushButton.clicked.connect(self.trik)
def trik(self):
self.aboutshow1 = AboutUs()
self.aboutshow1.show()
def obr(self,strs):
if strs:
self.ui.lineEdit.setText(strs)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = StartMyMain()
myapp.show()
sys.exit(app.exec_())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question