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的所有剩余空间。
在这部分教程中我们使用了菜单、工具栏、状态栏和主程序窗体。
- PyQt5教程(二)——菜单与工具栏
- PyQt5教程——菜单和工具栏(3)
- pyQt5-菜单和工具栏
- PyQt5中的菜单和工具栏
- PyQt5初级教程--PyQt5中的菜单和工具栏[4/13]
- PyQt5:PyQt5中的菜单和工具栏、主窗口(7)
- PyQt5教程-07-菜单栏、工具栏、状态栏
- PyQt5教程(四)——事件与信号
- wxPython菜单与工具栏
- WPF菜单与工具栏
- QT菜单与工具栏
- wxPython菜单与工具栏
- C#程序设计(二十八)----菜单与工具栏
- PyQt5教程(三)——布局管理
- PyQt5教程(五)——对话框
- PyQt5教程(六)——控件
- PyQt5教程(七)——控件(II)
- PyQt5教程(八)——拖放
- ToggleButton按钮
- hdu 4549 M斐波那契数列
- KMP算法
- (NYoj 325 zb的生日 和 456 邮票分你一半) 两个一样的题目不同的处理方法。(注意看题目中参数的较小值)
- Hdu 3691 Nubulsa Expo(无向图最小割)
- PyQt5教程(二)——菜单与工具栏
- git clone 一些简单笔记
- jquery截取图片后,ajax异步提交该图片
- Mysql SQL基础整理
- 智能厨房重构-项目分包
- 创意吃鱼法
- 那话
- 写给小白的Linux基础
- 图的基本存储的基本方式四