全局变量不能以对话框形式工作

时间:2019-06-06 13:17:23

标签: python-3.x pyqt5

我显示了主对话框。在对话框中,我更改全局变量的值。但是在对话框关闭后,全局变量不会更改该值。

我使用PyQt5。这是我称为对话框并更改变量值的代码。我使用varA来存储对话框结果。

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

对话框ui

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

在对话框中。我使用:

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

编辑:完整代码 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()

1 个答案:

答案 0 :(得分:0)

不建议更改在QT Designer中创建的文件。

没有必要或不建议使用全局变量。我还建议阅读Why are global variables evil

尝试一下:

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