我要做的是从主窗口菜单打开一个对话框,在这个对话框中,有一些输入,要么是linetextedit,要么是spinbox,或者是组合框。。。有一个按钮可以关闭对话框并将数据传递到主窗口。在主窗口中,执行一些操作。在我做的例子中,操作是将对话框中的两个数字相加显示在主窗口中,将一个txt文件写入本地磁盘并使用QDesktopServices打开该文件。在
即使不优雅,我做了这个工作,但我发现在对话框中,添加和显示以及打开的外部文件仍然执行,即使我使用右上角的“x”关闭对话框。我只想将函数链接到按钮,而不是关闭事件。在
在这里,我粘贴从ui转换过来的py文件和主文件。在
在主窗口UI.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))
在对话框ui.py在
^{pr2}$以及主.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)
另外,如果有人能纠正不太标准的部分,我也很感激。在
你可以使用信号和插槽。在
相关问题 更多 >
编程相关推荐