PyQt5中在主窗口MainWindow打开Dialog并传递信息

问题说明



最开始使用的是show()方法打开窗口,但是这时候主从窗口没有关联。Dialog未关闭的情况下就可以点击mainwindow。这种方式很不友好。在StackOverflow中知道应该用exec_()exec()会返回DialogAccept或者Reject等属性。判断返回值后即可进行返回值操作。

完整代码



  • dialog
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'edit_player.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_EditPlayer(object):
    def setupUi(self, EditPlayer):
        EditPlayer.setObjectName("EditPlayer")
        EditPlayer.resize(326, 202)
        EditPlayer.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
        EditPlayer.setFixedSize(EditPlayer.width(), EditPlayer.height())
        self.buttonBox = QtWidgets.QDialogButtonBox(EditPlayer)
        self.buttonBox.setGeometry(QtCore.QRect(30, 140, 261, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setCenterButtons(True)
        self.buttonBox.setObjectName("buttonBox")
        self.player_1 = QtWidgets.QLineEdit(EditPlayer)
        self.player_1.setGeometry(QtCore.QRect(30, 30, 113, 20))
        self.player_1.setMaxLength(4)
        self.player_1.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.player_1.setObjectName("player_1")
        self.player_2 = QtWidgets.QLineEdit(EditPlayer)
        self.player_2.setGeometry(QtCore.QRect(180, 30, 113, 20))
        self.player_2.setMaxLength(4)
        self.player_2.setObjectName("player_2")
        self.player_3 = QtWidgets.QLineEdit(EditPlayer)
        self.player_3.setGeometry(QtCore.QRect(30, 80, 113, 20))
        self.player_3.setMaxLength(4)
        self.player_3.setObjectName("player_3")
        self.player_4 = QtWidgets.QLineEdit(EditPlayer)
        self.player_4.setGeometry(QtCore.QRect(180, 80, 113, 20))
        self.player_4.setMaxLength(4)
        self.player_4.setObjectName("player_4")

        self.retranslateUi(EditPlayer)
        self.buttonBox.accepted.connect(EditPlayer.accept)
        # self.buttonBox.accepted.connect(self.log)
        self.buttonBox.rejected.connect(EditPlayer.reject)
        QtCore.QMetaObject.connectSlotsByName(EditPlayer)

    def retranslateUi(self, EditPlayer):
        _translate = QtCore.QCoreApplication.translate
        EditPlayer.setWindowTitle(_translate("EditPlayer", "Dialog"))


class EditPlayerDialog(QtWidgets.QDialog, Ui_EditPlayer):
    def __init__(self, parent=None):
        super(EditPlayerDialog, self).__init__(parent=parent)
        self.setupUi(self)

    def get_player(self):
        player = {}
        for i in range(1, 5):
            player_line_edit_list = getattr(self, 'player_%d' % i)
            player[i] = player_line_edit_list.text()
        return player


# if __name__ == '__main__':
#     app = QtWidgets.QApplication(sys.argv)
#     ui = EditPlayerDialog()
#     ui.show()
#     sys.exit(app.exec_())

  • mainWindow
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from edit_player import EditPlayerDialog


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(490, 368)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(60, 40, 256, 192))
        self.textBrowser.setObjectName("textBrowser")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(370, 110, 75, 71))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 490, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        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", "PushButton"))


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent=parent)
        # self.player_dialog = EditPlayerDialog()
        self.setupUi(self)

    def button_click(self):
        self.pushButton.clicked.connect(self.open_dialog)

    def open_dialog(self):
        player_dialog = EditPlayerDialog()
        if player_dialog.exec_() == QtWidgets.QDialog.Accepted:
            player_dict = player_dialog.get_player()
            self.textBrowser.insertHtml(str(player_dict) + '<br>')
        else:
            print('test')


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ui = MainWindow()
    ui.button_click()
    ui.show()
    sys.exit(app.exec_())

ps



本来以为不可以直接循环生成元素的,试了一下发现可行.数组或者变量都可以成功...
        #self.button_list = []
        for i in range(0, 3):
            # self.button_list.append(i)
            # self.button_list[i] = QtWidgets.QPushButton(self.centralwidget)
            # y = 15 + i*85
            # self.button_list[i].setGeometry(QtCore.QRect(370, y, 75, 71))
            # self.button_list[i].setObjectName("pushButton_%d" % i)
            # self.button_list[i].setText('button_%d'% i)
            y = 15 + i*85
            button_name = 'button_%d' % i
            self.button_name= QtWidgets.QPushButton(self.centralwidget)

            self.button_name.setGeometry(QtCore.QRect(370, y, 75, 71))
            self.button_name.setObjectName("pushButton_%d" % i)
            self.button_name.setText('button_%d'% i)