由 PyQt Designer 创建的菜单栏

Menubar created by PyQt Designer

我在 PyQt-Designer 中设计了一个表面并将其转换为 .py 不,我正在尝试 link 菜单栏(按钮)的功能。有打开,保存,关闭之类的东西...... 我尝试了很多,但都没有成功,我希望你能帮我连接一个简单的功能到菜单栏中的打开按钮。

例如 linked 到菜单栏的功能 菜单栏->打开->功能(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_())

首先我建议你阅读the docs,建议不要修改Qt Designer生成的class,但你必须创建另一个class继承自一个widget并且使用Qt Designer提供的class作为接口。

另一方面,没有必要使用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_())