简体   繁体   中英

How do I add a PyQt menubar to my program?

I'm currently working on a user interface for my Python program with PyQt5.

In my program I first read a csv file and then create a dataframe with pandas. This also works so far.

Now I want to create a user interface with PyQt5. This interface should contain a menubar with two buttons ("File" and "Run") and should display my dataframe (see picture) .

How it finally should look like

With the first button I want to read my csv file and with the other I want to start the program.

Currently I can only display a dataframe with my user interface (see picture) .

How it looks right now

That is my code at the moment with a random dataframe

import sys
import pandas as pd
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5.QtCore import QAbstractTableModel, Qt

df = pd.DataFrame({'A': ['AAAAAAAA11111111', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz'],
                   'B': ['BBBBBBBB11111111', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz'],
                   'C': ['CCCCCCCC11111111', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz'],        
                   'D': ['CCCCCCCC11111111', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz']})

class pandasModel(QAbstractTableModel):

    def __init__(self, data):
        QAbstractTableModel.__init__(self)
        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parent=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None

if __name__ == '__main__':
    app = QApplication(sys.argv)
    model = pandasModel(df)
    view = QTableView()
    view.setModel(model)
    view.resize(800, 300)
    view.horizontalHeader().setSectionResizeMode(1) 
    view.show()
    sys.exit(app.exec_())

In order to maintain my desired user interface, I have created a PyQt Designer interface with a menu bar (see picture). But I have no idea how to link these two interfaces.

Design from PyQt_Designer

Code from PyQt Designer:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(600, 300)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../../Desktop/index.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)

        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(0, 0, 600, 300))
        self.tableWidget.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.CrossCursor))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 600, 21))
        self.menubar.setObjectName("menubar")
        self.menuSelect = QtWidgets.QMenu(self.menubar)
        self.menuSelect.setObjectName("menuSelect")

        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuSelect.menuAction())

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Analyser"))
        self.menuSelect.setTitle(_translate("MainWindow", "Select File"))


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_())

How can I add a menubar to my program or alternatively how to link my program to the code of the PyQt Designer?

First of all, read the PyQt documentation about using Designer .
Long story short, you should never edit the python files generated by pyuic, but create your code and use the generated files as modules. Read the documentation and you'll understand (you might want to look into the third example).

Remove the QTableWidget you added (as it is the wrong widget type), drag a QTableView to the main window, set a layout (right click on an empty space outside the table and within the window, and select one of Horizontal or Vertical in the "Lay out" submenu), save and rebuild the UI file.

At this point, following the procedure in the documentation, you just set the model from your class __init__ :

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QAbstractTableModel, Qt
from ui_mainwindow import Ui_MainWindow

class pandasModel(QAbstractTableModel):
    # ...

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.model = pandasModel(df)
        self.tableView.setModel(self.model)

        self.actionOpen.triggered.connect(self.doSomething)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM