我想做的是从主窗口菜单中打开一个对话框,在该对话框中,有一些输入,即linetextedit或spinbox或comboboxes ...,还有一个按钮可以关闭对话框并将数据传递到主窗口。在主窗口中,进行了一些操作。在我制作的示例中,操作是将对话框中的两个数字加在一起显示在主窗口中,将txt文件写入本地磁盘,然后使用QDesktopServices打开文件。
即使不是很优雅,我也能做到这一点,但是我发现,即使我使用右上角的'x'来关闭对话框,在对话框中的仍会执行添加和显示以及打开的外部文件< / strong>。我只想将功能链接到按钮,而不是关闭事件。
在这里,我将从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)
如果有人可以纠正似乎不太标准的零件,我也将不胜感激。
答案 0 :(得分:0)
您的代码有几个错误:
第dlg.pushButton.clicked.connect(self.getDialogInfo(dlg))
行没有意义,让我们分析一下,什么结果self.getDialogInfo(dlg)
,函数getDialogInfo
执行任务,但不返回等于return的值None
,因此初始代码等于dlg.pushButton.clicked.connect(None)
,它可以执行您想要的任务,但是不需要连接。
当您调用exec_()
时,该对话框将返回一个代码,该代码用于区分是否接受该任务,因为这是对话框的任务。该代码仅在您调用关闭窗口的accept()
或reject()
方法时返回,因此必须调用close()
而不是按钮调用accept()
,因此当您关闭窗口时使用X按钮的窗口将在内部调用reject()
,因此您可以区分每个按钮。
请勿使用sum,因为sum是函数的名称,并且被视为保留字,否则将来可能会引起问题。
用于打开文件,因为它在退出时会自动关闭。
不要手动连接路径,因为连接取决于平台,因此最好使用库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)
答案 1 :(得分:0)
您可以使用信号和插槽。
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)