Search code examples
pythonwidget

Passing variables between an open window and a window that hasn't been opened yet


I am currently stuck on a problem of passing variables between two scripts in a python GUI code. I think I have figured out that the variable is not getting sent from the main widget to the secondary widget but I'm not sure how to get around it.

Using designer, I've mocked up two example scripts mainWidget.py

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox

from calledWidget import Ui_CalledWindow

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(640, 170)
        Form.setWindowTitle("Form")
        
        
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        
        
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setObjectName("lineEdit")
        self.verticalLayout.addWidget(self.lineEdit)
        
        
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("PushButton")
        self.verticalLayout.addWidget(self.pushButton)
        self.pushButton.clicked.connect(self.passString)       
        
    def passString(self):
        stringToPass = self.lineEdit.text()
        print(stringToPass)
        
        openingDialog = QMessageBox()
        openingDialog.setWindowTitle("Warning")
        openingDialog.setText("Are sure you want to pass the string: {}?".format(stringToPass))
        openingDialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        reply = openingDialog.exec()
        
        if reply == QMessageBox.Yes:
            self.stringOut = stringToPass
            
            self.newWindow = QtWidgets.QDialog()
            self.ui = Ui_CalledWindow()
            self.ui.setupUi(self.newWindow)
            self.newWindow.show()
        
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

and calledWidget.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_CalledWindow(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(612, 178)
        Dialog.setWindowTitle("Dialog")
        
        
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        
        
        self.listView = QtWidgets.QListWidget(Dialog)
        self.listView.setObjectName("listView")
        self.verticalLayout.addWidget(self.listView)
        self.listView.addItem(self.stringOut)
        
        
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        
        
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.verticalLayout.addWidget(self.buttonBox)

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


where the passString() method in mainWidget.py opens the dialog window calledWidget.py. The line self.listView.addItem(self.stringOut) in the latter should be enough to do it but I get an object attribution error instead. Any ideas?


Solution

  • You can get around this problem by using the shelve method given in example by https://www.tutorialspoint.com/python-object-persistence-shelve#:~:text=The%20shelve%20module%20in%20Python's,stored%20in%20a%20disk%20file.