Помогите разобраться, работаю с PyQT5 и с Qt Designer.
Есть два окна, я передаю значение из ComboBox
первого окна в label
второго окна, это без проблем.
Дальше мне нужно получить это значение во втором окне из label
и провести с ним некоторые манипуляции, но при попытке его получить, мне всегда возвращается его изначальное значение, как будто на форму оно и не передалось.
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from SecondWindow import SecondWindowView
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(313, 375)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(90, 60, 121, 41))
font = QtGui.QFont()
font.setPointSize(13)
self.comboBox.setFont(font)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(80, 200, 141, 71))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
FirstWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(FirstWindow)
self.statusbar.setObjectName("statusbar")
FirstWindow.setStatusBar(self.statusbar)
self.retranslateUi(FirstWindow)
QtCore.QMetaObject.connectSlotsByName(FirstWindow)
def retranslateUi(self, FirstWindow):
_translate = QtCore.QCoreApplication.translate
FirstWindow.setWindowTitle(_translate("FirstWindow", "MainWindow"))
self.comboBox.setItemText(0, _translate("FirstWindow", "User1"))
self.comboBox.setItemText(1, _translate("FirstWindow", "User2"))
self.comboBox.setItemText(2, _translate("FirstWindow", "User3"))
self.pushButton.setText(_translate("FirstWindow", "SendData"))
class FirstWindowView(QtWidgets.QMainWindow, Ui_FirstWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.open_second_window)
def open_second_window(self):
self.sw = SecondWindowView()
val = self.comboBox.currentText()
self.sw.label_get_user.setText(val)
self.hide()
self.sw.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
fw = FirstWindowView()
fw.show()
sys.exit(app.exec_())
SecondWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_SecondWindow(object):
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(320, 384)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_user = QtWidgets.QLabel(self.centralwidget)
self.label_user.setGeometry(QtCore.QRect(10, 80, 71, 41))
font = QtGui.QFont()
font.setPointSize(18)
self.label_user.setFont(font)
self.label_user.setObjectName("label_user")
self.label_get_user = QtWidgets.QLabel(self.centralwidget)
self.label_get_user.setGeometry(QtCore.QRect(100, 70, 251, 61))
font = QtGui.QFont()
font.setPointSize(14)
self.label_get_user.setFont(font)
self.label_get_user.setStyleSheet("color:red")
self.label_get_user.setObjectName("label_get_user")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(40, 250, 251, 71))
self.lineEdit.setObjectName("lineEdit")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(40, 212, 241, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
SecondWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.label_user.setText(_translate("SecondWindow", "User:"))
self.label_get_user.setText(_translate("SecondWindow", "UserFromFirstWindow"))
self.label_3.setText(_translate("SecondWindow", "GetItButNeedUserFromFirstWindow"))
class SecondWindowView(QtWidgets.QMainWindow, Ui_SecondWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.lineEdit.setReadOnly(True)
self.lineEdit.setText(self.label_get_user.text())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
sw = SecondWindowView()
sw.show()
sys.exit(app.exec_())