PyQt5教程(二)——菜单与工具栏

来源:互联网 发布:吃饭前拍照知乎 编辑:程序博客网 时间:2024/05/18 01:38

我们将在这部分教程中创建菜单与工具栏。一个菜单就是位于菜单栏中的一组命令。应用的工具栏放置了带有按钮的常用命令。

主窗体

QMainWindow类提供了一个主程序窗体。通过它可以创建带有状态栏、工具栏与菜单栏的传统应用程序。

状态栏

状态栏是用于显示状态信息的控件。

import sysfrom PyQt5.QtWidgets import QMainWindow, QApplicationclass Example(QMainWindow):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.statusBar().showMessage("Ready")        self.setGeometry(300, 300, 250, 150)        self.setWindowTitle("Statusbar")        self.show()if __name__ == "__main__":    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

可以通过QMainWindow创建状态栏控件。

self.statusBar().showMessage('Ready')

我们需要调用QtGui.QMainWindow的statusBar()方法来创建状态栏。第一次调用该方法会创建一个状态栏对象,之后的调用都会返回这个状态栏对象。showMessage()会将消息展示在状态栏。
这里写图片描述

菜单栏

菜单栏是GUI程序的标配。它是一组位于不同菜单内的命令集。(Mac系统会以不同的方式处理菜单栏,但添加menubar.setNativeMenuBar(False)这行代码后可以得到一致的结果。)

import sysfrom PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplicationfrom PyQt5.QtGui import QIconclass Example(QMainWindow):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        exitAction = QAction(QIcon("exit.png"), "&Exit", self)        exitAction.setShortcut("Ctrl+Q")        exitAction.setStatusTip("Exit application")        exitAction.triggered.connect(qApp.quit)        self.statusBar()        menubar = self.menuBar()        fileMenu = menubar.addMenu("&File")        fileMenu.addAction(exitAction)        self.setGeometry(300, 300, 300, 200)        self.setWindowTitle("Menubar")        self.setWindowIcon(QIcon("SmartMedicineKit.png"))        self.show()if __name__ == "__main__":    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这个例子中我们创建了一个带有一个菜单的菜单栏。这个菜单中只有一个动作,当触发后会使程序停止。这里也创建了状态栏。可以使用Ctrl+Q快捷键触发这个动作。

exitAction = QAction(QIcon('exit.png'), '&Exit', self)        exitAction.setShortcut('Ctrl+Q')exitAction.setStatusTip('Exit application')

QAction是菜单栏、工具栏或自定义快捷键中可以执行的动作的抽象表示。上面这三行代码创建了一个带有特定图标与‘Exit’标签的动作,而且还为这个动作定义了一个快捷键。第三个代码为这个动作设置了状态提示,当鼠标悬停在这个菜单项上时状态提示会显示在状态栏。

exitAction.triggered.connect(qApp.quit)

当点击这个动作时会发出triggered信号。这个信号连接到了QApplication的quit()方法。从而使程序停止。

menubar = self.menuBar()fileMenu = menubar.addMenu('&File')fileMenu.addAction(exitAction)

menuBar()方法会创建一个菜单栏。我们在菜单栏中创建了一个file菜单并为其添加了exitAction。
这里写图片描述

工具栏

菜单为应用程序中的所有命令进行分组。工具栏为常用命令提供了快速的访问方式。

import sysfrom PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplicationfrom PyQt5.QtGui import QIconclass Example(QMainWindow):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        exitAction = QAction(QIcon("exit.png"), "Exit", self)        exitAction.setShortcut("Ctrl+Q")        exitAction.triggered.connect(qApp.quit)        self.toolbar = self.addToolBar("Exit")        self.toolbar.addAction(exitAction)        self.setGeometry(300, 300, 300, 200)        self.setWindowTitle("Toolbar")        self.setWindowIcon(QIcon("SmartMedicineKit.png"))        self.show()if __name__ == "__main__":    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这个例子中我们创建了一个简单的工具栏。这个工具栏中有一个退出动作,当触发后会使程序退出。

exitAction = QAction(QIcon('exit24.png'), 'Exit', self)exitAction.setShortcut('Ctrl+Q')exitAction.triggered.connect(qApp.quit)

与上面菜单栏示例类似,我们创建了一个QAction对象。这个对象也有标签、图标和快捷键。QtGui.QMainWindow的quit()方法与triggered信号相连。

self.toolbar = self.addToolBar('Exit')self.toolbar.addAction(exitAction)

这里我们创建了一个工具栏并在其中添加了一个QAction对象。
这里写图片描述

组装起来

在这节教程的最后,我们将创建一个菜单栏、工具栏与状态栏。我们也会创建一个中心部件。

import sysfrom PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplicationfrom PyQt5.QtGui import QIconclass Example(QMainWindow):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        textEdit = QTextEdit()        self.setCentralWidget(textEdit)        exitAction = QAction(QIcon("exit24.png"), "Exit", self)        exitAction.setShortcut("Ctrl+Q")        exitAction.setStatusTip("Exit application")        exitAction.triggered.connect(self.close)        self.statusBar()        menubar = self.menuBar()        fileMenu = menubar.addMenu("&File")        fileMenu.addAction(exitAction)        toolbar = self.addToolBar("Exit")        toolbar.addAction(exitAction)        self.setGeometry(300, 300, 350, 250)        self.setWindowTitle("Main Window")        self.setWindowIcon(QIcon("SmartMedicineKit.png"))        self.show()if __name__ == "__main__":    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

这段代码创建了一个包含菜单栏、工具栏与状态栏的传统GUI程序。

textEdit = QTextEdit()self.setCentralWidget(textEdit)

这里我们创建了一个TextEdit控件。我们将它设置为QMainWindow的中心控件。中心控件会占用QMainWindow的所有剩余空间。
这里写图片描述


在这部分教程中我们使用了菜单、工具栏、状态栏和主程序窗体。

1 0
原创粉丝点击