0

I show a dialog from main. In the dialog, I change the value of a global variable. But after the dialog closes, the global variable does not change the value.

I use PyQt5. Here is the code I call the dialog and change the variable value. I use varA to store dialog result.

def Clicked(self):
    global varA
    dialog = clssDialog()
    dialog.exec_()
    print(varA)

dialog ui

class clssDialog(QDialog):
    def __init__(self):
        super(clssDialog, self).__init__()
        #some code

In dialog. I use:

def btnClosed(self):
    global varA
    varA=value
    self.close()

Edit: Full code main.py

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

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(219, 62)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.Clicked)
        self.verticalLayout.addWidget(self.pushButton)
        MainWindow.setCentralWidget(self.centralwidget)
        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", "Open form"))

    def Clicked(self):
        global varA
        varA=""
        dialog = clssDialog()
        dialog.exec_()
        print("value: " + varA)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

dialog.py

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

class clssDialog(QDialog):
    def __init__(self):
        super(clssDialog, self).__init__()
        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.btnClosed)
        self.verticalLayout.addWidget(self.pushButton)
        self.setWindowTitle("Dialog")
        self.pushButton.setText("Close")

    def btnClosed(self):
        global varA
        varA="123"
        self.close()
StackUser
  • 25
  • 8
  • 1
    Please, provide a minimal and complete example – Dimitry Ernot Jun 06 '19 at 13:20
  • Because it is in the whole program, the code is too long and not suitable to upload. I created a new project and put in the full code. – StackUser Jun 06 '19 at 13:49
  • Don't use global variables! – BlackJack Jun 06 '19 at 14:57
  • @BlackJack I understand the problem of global variables. But I do not discriminate against it. In some cases, I think using a more reasonable global variable. I use global variables because I don't know how to change the control value in main from dialog. But with S. Nick's help, I understand better. But not yet applied to my program successfully. – StackUser Jun 06 '19 at 15:44

1 Answers1

0

It is not recommended to change the file created in QT Designer.

It is not necessary or advisable to use global variables. I also recommend reading Why are global variables evil?

Try it:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

#from dialog import *

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(219, 62)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.Clicked)
        self.verticalLayout.addWidget(self.pushButton)
        MainWindow.setCentralWidget(self.centralwidget)
        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", "Open form"))


#    def Clicked(self):
#        global varA
#        varA=""
#        dialog = ClssDialog()
#        dialog.exec_()
#        print("value: " + varA)


class ClssDialog(QDialog):
#    def __init__(self):
#        super(ClssDialog, self).__init__()
    def __init__(self, parent=None):
        super(ClssDialog, self).__init__(parent)
        self.parent = parent

        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.btnClosed)
        self.verticalLayout.addWidget(self.pushButton)
        self.setWindowTitle("Dialog")
        self.pushButton.setText("Close")

    def btnClosed(self):
#        global varA
        self.parent.varA = "123"
        self.close()


class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()

        self.setupUi(self)    

        self.varA = ""        

    def Clicked(self):
#        global varA
#        varA=""
        dialog = ClssDialog(self)
        dialog.exec_()
        print("value: " + self.varA)       


if __name__ == "__main__":
    app = QApplication(sys.argv)
    view = ExampleApp()
    view.show()
    sys.exit(app.exec_()) 

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • Thanks for the help but I don't want to use one a file because there are many other windows. I am working on my application and I have also searched a lot, from calling windows by individual files and many other things. With the above problem, are you able to use it without including a file? I use Google translate. – StackUser Jun 06 '19 at 14:49
  • 1
    Thank you @S. Nick. I have done it based on your code. – StackUser Jun 06 '19 at 14:59
  • I change another global variable in the form of a list so the code works fine. Do you know why? – StackUser Jun 07 '19 at 01:29