如何 link 对话框按钮到主 window 函数,以及如何将关闭事件与函数分开

How to link the dialog button to main window function and also make close event separate from the function

我想做的是从主窗口菜单打开一个对话框,在该对话框中,有一些输入,linetextedit 或 spinbox,或组合框...还有一个按钮可以关闭对话框并将数据传递给主窗口。在主窗口中,进行了一些操作。在我做的例子中,操作是将对话框中的两个数字加在一起显示在主窗口中,将一个 txt 文件写入本地磁盘并使用 QDesktopServices 打开该文件。

即使不优雅,我也做了这个工作,但我发现 在对话框中,即使我使用 [=31= 关闭对话框,添加和显示以及打开的外部文件仍然执行]右上角。我只想 link 按钮的功能而不是关闭事件。

这里我粘贴了ui和主文件中转换后的py文件。

mainwindowui.py

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(486, 497)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 486, 22))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpenLocal = QtWidgets.QAction(MainWindow)
        self.actionOpenLocal.setObjectName("actionOpenLocal")
        self.menuFile.addAction(self.actionOpenLocal)
        self.menubar.addAction(self.menuFile.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
        self.label.setText(QtWidgets.QApplication.translate("MainWindow", "Summation", None, -1))
        self.menuFile.setTitle(QtWidgets.QApplication.translate("MainWindow", "File", None, -1))
        self.actionOpenLocal.setText(QtWidgets.QApplication.translate("MainWindow", "OpenLocal", None, -1))

dialogui.py

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(320, 237)
        self.gridLayout = QtWidgets.QGridLayout(Dialog)
        self.gridLayout.setObjectName("gridLayout")
        self.labelA = QtWidgets.QLabel(Dialog)
        self.labelA.setObjectName("labelA")
        self.gridLayout.addWidget(self.labelA, 0, 0, 1, 1)
        self.alineEdit = QtWidgets.QLineEdit(Dialog)
        self.alineEdit.setObjectName("alineEdit")
        self.gridLayout.addWidget(self.alineEdit, 0, 1, 1, 1)
        self.labelB = QtWidgets.QLabel(Dialog)
        self.labelB.setObjectName("labelB")
        self.gridLayout.addWidget(self.labelB, 1, 0, 1, 1)
        self.blineEdit = QtWidgets.QLineEdit(Dialog)
        self.blineEdit.setObjectName("blineEdit")
        self.gridLayout.addWidget(self.blineEdit, 1, 1, 1, 1)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 2)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtWidgets.QApplication.translate("Dialog", "Dialog", None, -1))
        self.labelA.setText(QtWidgets.QApplication.translate("Dialog", "inputA", None, -1))
        self.labelB.setText(QtWidgets.QApplication.translate("Dialog", "inputB", None, -1))
        self.pushButton.setText(QtWidgets.QApplication.translate("Dialog", "OpenPdf", None, -1)) 

和main.py

import sys
import os

from PySide2 import QtCore, QtGui, QtWidgets
from mainwindowui import Ui_MainWindow
from dialogui import Ui_Dialog


class fileDialog(QtWidgets.QDialog,Ui_Dialog):
    def __init__(self,parent):
        super().__init__(parent)
        self.setupUi(self)
        self.setWindowTitle("Open File")
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.close)


class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.actionOpenLocal.triggered.connect(self.startDialog)
        self.show()

    def startDialog(self):
        dlg = fileDialog(self)
        dlg.exec_()
        dlg.pushButton.clicked.connect(self.getDialogInfo(dlg))


    def getDialogInfo(self,dialogue):
        self.avalue = float(dialogue.alineEdit.text())
        self.bvalue = float(dialogue.blineEdit.text())
        sum = str(self.avalue+self.bvalue)
        self.lineEdit.setText(sum)
        file = open("result.txt","w")
        file.write(sum)
        file.close()

        QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(currentdir +"/result.txt"))


if __name__=='__main__':
    app = QtWidgets.QApplication(sys.argv)
    currentdir = os.getcwd()
    mainWin = MainWindow()
    ret = app.exec_()
    sys.exit(ret)

另外,如果有人能指正那些看起来不太标准的部分,我也很感激。

您的代码有几个错误:

  • dlg.pushButton.clicked.connect(self.getDialogInfo(dlg))没有意义,我们分析一下,什么结果有self.getDialogInfo(dlg),函数getDialogInfo执行了一个任务但没有return 等于 return 和 None 的任何值,因此初始代码等于 dlg.pushButton.clicked.connect(None),它可以完成您想要的任务,但不需要连接。

  • 对话框 return 是您调用 exec_() 时的代码,它用于区分任务是否被接受,因为这是对话框的任务。只有当您调用关闭 window 的 accept()reject() 方法时,该代码才会被 returned,因此您必须调用 [=而不是按钮调用 close() =17=],因此当您使用 X 按钮关闭 window 时,它会在内部调用 reject(),因此您可以区分每个按钮。

  • 不要使用 sum,因为它是一个函数名,它被认为是一个保留字,这样做以后可能会出现问题。

  • 使用 with 打开文件,因为它会在文件退出时自动关闭文件。

  • 不要手动拼接路径,拼接依赖平台,最好使用库提供的函数os.

  • 使用新的连接语法,可读性更好

综合以上,得到如下解法:

import os
import sys

from PySide2 import QtCore, QtGui, QtWidgets

from mainwindowui import Ui_MainWindow
from dialogui import Ui_Dialog


class fileDialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self,parent):
        super().__init__(parent)
        self.setupUi(self)
        self.setWindowTitle("Open File")
        self.pushButton.clicked.connect(self.accept)


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.actionOpenLocal.triggered.connect(self.startDialog)
        self.show()

    def startDialog(self):
        dlg = fileDialog(self)
        if dlg.exec_() == QtWidgets.QDialog.Accepted:
            self.getDialogInfo(dlg)

    def getDialogInfo(self, dialogue):
        self.avalue = float(dialogue.alineEdit.text())
        self.bvalue = float(dialogue.blineEdit.text())
        res = str(self.avalue+self.bvalue)
        self.lineEdit.setText(res)
        with open("result.txt","w") as file:
            file.write(res)
        QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(os.path.join(currentdir,"result.txt")))


if __name__=='__main__':
    app = QtWidgets.QApplication(sys.argv)
    currentdir = os.getcwd()
    mainWin = MainWindow()
    ret = app.exec_()
    sys.exit(ret)

您可以使用信号和槽。

import sys
import os

from PySide2 import QtCore, QtGui, QtWidgets
from mainwindowui import Ui_MainWindow
from dialogui import Ui_Dialog


class fileDialog(QtWidgets.QDialog,Ui_Dialog):
    sig_complete = QtCore.Signal(dict)
    def __init__(self,parent):
        super().__init__(parent)
        self.setupUi(self)
        self.setWindowTitle("Open File")
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.slt_save)

    def slt_save(self):
        self.sig_complete.emit({"aline": self.alineEdit.text(), "bline": self.blineEdit.text()})
        self.close()

class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.actionOpenLocal.triggered.connect(self.startDialog)
        self.show()

    def startDialog(self):
        dlg = fileDialog(self)
        dlg.sig_complete.connect(self.getDialogInfo)
        dlg.exec_()

    def getDialogInfo(self,data_dict):
        self.avalue = float(data_dict["aline"])
        self.bvalue = float(data_dict["bline"])
        sum = str(self.avalue+self.bvalue)
        self.lineEdit.setText(sum)
        file = open("result.txt","w")
        file.write(sum)
        file.close()
        QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(currentdir +"/result.txt"))


if __name__=='__main__':
    app = QtWidgets.QApplication(sys.argv)
    currentdir = os.getcwd()
    mainWin = MainWindow()
    ret = app.exec_()
    sys.exit(ret)