我在 PyQt-Designer 中设计了一个曲面并将其转换为.py不,我正在尝试将功能链接到菜单栏-(按钮)。有类似打开,保存,关闭的东西。我已经尝试了很多,但没有成功,我希望你能帮我把一个简单的功能连接到菜单栏中的打开按钮。
例如,链接到菜单栏的函数菜单栏->打开->功能(open_path):
def open_path():
root= Tk()
Pfad=askdirectory()
root.destroy
感谢您的帮助!
这是代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import os
from tkinter import *
from tkinter.filedialog import askdirectory
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Auswertung Brechzahlbestimmung")
MainWindow.resize(1205, 641)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_5 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_5.setObjectName("gridLayout_5")
self.gridLayout_3 = QtWidgets.QGridLayout()
self.gridLayout_3.setObjectName("gridLayout_3")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.histogram = QtWidgets.QGraphicsView(self.centralwidget)
self.histogram.setMinimumSize(QtCore.QSize(182, 126))
self.histogram.setMaximumSize(QtCore.QSize(16777215, 126))
self.histogram.setObjectName("histogram")
self.verticalLayout_2.addWidget(self.histogram)
self.gridLayout_3.addLayout(self.verticalLayout_2, 2, 2, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout_3.addWidget(self.label, 0, 2, 1, 1)
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.gridLayout_3.addWidget(self.horizontalSlider, 2, 0, 1, 1)
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setObjectName("label_4")
self.gridLayout_3.addWidget(self.label_4, 0, 0, 1, 1)
self.gridLayout_4 = QtWidgets.QGridLayout()
self.gridLayout_4.setObjectName("gridLayout_4")
self.gridLayout_3.addLayout(self.gridLayout_4, 1, 4, 1, 1)
self.seitenansicht = QtWidgets.QGraphicsView(self.centralwidget)
self.seitenansicht.setMinimumSize(QtCore.QSize(537, 407))
self.seitenansicht.setObjectName("seitenansicht")
self.gridLayout_3.addWidget(self.seitenansicht, 1, 2, 1, 1)
self.verticalSlider = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.gridLayout_3.addWidget(self.verticalSlider, 1, 1, 1, 1)
self.draufsicht = QtWidgets.QGraphicsView(self.centralwidget)
self.draufsicht.setMinimumSize(QtCore.QSize(537, 407))
self.draufsicht.setObjectName("draufsicht")
self.gridLayout_3.addWidget(self.draufsicht, 1, 0, 1, 1)
self.referenz = QtWidgets.QGraphicsView(self.centralwidget)
self.referenz.setMinimumSize(QtCore.QSize(70, 0))
self.referenz.setMaximumSize(QtCore.QSize(70, 16777215))
self.referenz.setObjectName("referenz")
self.gridLayout_3.addWidget(self.referenz, 1, 3, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout_3.addWidget(self.label_2, 0, 3, 1, 1)
self.gridLayout_5.addLayout(self.gridLayout_3, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
#Here begins the Menubar
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1205, 20))
self.menubar.setObjectName("menubar")
self.menuDatei = QtWidgets.QMenu(self.menubar)
self.menuDatei.setObjectName("menuDatei")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionExport_Picture = QtWidgets.QAction(MainWindow)
self.actionExport_Picture.setObjectName("actionExport_Picture")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuDatei.addAction(self.actionSave)
self.menuDatei.addAction(self.actionOpen)
self.menuDatei.addSeparator()
self.menuDatei.addAction(self.actionExport_Picture)
self.menuDatei.addSeparator()
self.menuDatei.addAction(self.actionExit)
self.menubar.addAction(self.menuDatei.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Auswertung Brechzahlbestimmung"))
self.label.setText(_translate("MainWindow", "Schnitt"))
self.label_4.setText(_translate("MainWindow", "Draufsicht"))
self.label_2.setText(_translate("MainWindow", "n"))
self.menuDatei.setTitle(_translate("MainWindow", "Datei"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionExport_Picture.setText(_translate("MainWindow", "Export Picture"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
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_())
首先我建议你阅读文档,它建议不要修改Qt Designer生成的类,但你必须创建另一个继承自widget的类,并使用Qt Designer提供的类作为接口。
另一方面,没有必要使用 tkinter,Qt 提供了小部件来获取像 QFileDialog 这样的目录路径。
最后,您必须使用self.actionOpen的触发信号。
考虑到上述情况,解决方案是:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
# ...
def retranslateUi(self, MainWindow):
# ...
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.actionOpen.triggered.connect(self.open_file)
@QtCore.pyqtSlot()
def open_file(self):
fdirectory = QtWidgets.QFileDialog.getExistingDirectory(self, "Open Directory")
if fdirectory:
print(fdirectory)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())