PySide2 passing variable from another window

Viewed 16

I would like to transfer variables between two windows. The idea is to set (in the first window) the value that I will give in the second window. How can I return values ​​from a window on exit? Example Code:

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import sys

class AnotherWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(QRect(300, 300, 300, 100))
        self.lcd = QSpinBox()
        self.btn = QPushButton("Send")
        self.btn.clicked.connect(self.send)
        layout = QVBoxLayout()
        layout.addWidget(self.lcd)
        layout.addWidget(self.btn)
        self.setLayout(layout)
    
    def send(self):
        t = self.lcd.text()
        # return t
        self.close()


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(200, 100)
        self.lcd = QLCDNumber()
        self.btn = QPushButton("New Window")
        self.btn.clicked.connect(self.open_new)
        layout = QVBoxLayout()
        layout.addWidget(self.lcd)
        layout.addWidget(self.btn)
        self.setLayout(layout)

    def open_new(self):
        self.new_window = AnotherWindow()
        self.new_window.show()
        # self.lcd.display(t)


def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()
main()
0 Answers
Related