一. 状态栏

# -*- coding: utf-8 -*-
# @Time : 2018/12/22 12:37
# @Author : Bo
# @Email : mat_wu@163.com
# @File : demo3.py
# @Software: PyCharm
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication class 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()方法在状态栏上显示一条信息。

二  菜单栏

import sys
from PyQt5.QtWidgets import QAction,qApp,QApplication,QMainWindow
from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self):
super().__init__() self.initUI() def initUI(self): exitAct = QAction(QIcon('1.png'),'&Exit',self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit Application')
exitAct.triggered.connect(qApp.quit) self.statusBar() menubar = self.menuBar()
fileMenu = menubar.addMenu("&File")
fileMenu.addAction(exitAct) self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('simple menu')
self.show() if __name__ == '__main__': app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

我们创建了只有一个命令的菜单栏,这个命令就是终止应用。同时也创建了一个状态栏。而且还能使用快捷键Ctrl+Q退出应用。

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

QAction是菜单栏、工具栏或者快捷键的动作的组合。前面两行,我们创建了一个图标、一个exit的标签和一个快捷键组合,都执行了一个动作。第三行,创建了一个状态栏,当鼠标悬停在菜单栏的时候,能显示当前状态。

exitAct.triggered.connect(qApp.quit)

当执行这个指定的动作时,就触发了一个事件。这个事件跟QApplication的quit()行为相关联,所以这个动作就能终止这个应用。

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

menuBar()创建菜单栏。这里创建了一个菜单栏,并在上面添加了一个file菜单,并关联了点击退出应用的事件。

三 .子菜单

import sys
from PyQt5.QtWidgets import QAction, qApp, QApplication, QMainWindow, QMenu
from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self):
super().__init__() self.initUI() def initUI(self): exitAct = QAction(QIcon('1.png'),'&Exit',self)
exitAct.setShortcut('Ctrl+Q') #快捷键
exitAct.setStatusTip('Exit Application')
exitAct.triggered.connect(qApp.quit) impMenu = QMenu("Import",self)
impAct = QAction("Import mail",self)
impMenu.addAction(impAct) self.statusBar() menubar = self.menuBar()
fileMenu = menubar.addMenu("&File")
fileMenu.addAction(exitAct) newAct = QAction("new",self)
fileMenu.addAction(newAct)
fileMenu.addMenu(impMenu) self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('simple menu')
self.show() if __name__ == '__main__': app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

这个例子里,有两个子菜单,一个在file菜单下面,一个在file的import下面。

impMenu = QMenu('Import', self)

使用QMenu创建一个新菜单。

impAct = QAction('Import mail', self)
impMenu.addAction(impAct)

使用addAction添加一个动作。

四  . 勾选菜单,右键菜单

五 . 工具栏

六 . 主窗口

import sys
from PyQt5.QtWidgets import QAction, qApp, QApplication, QMainWindow, QMenu, QTextEdit
from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self):
super().__init__() self.initUI() def initUI(self): textEdit = QTextEdit()
self.setCentralWidget(textEdit) exitAct = QAction(QIcon('1.png'),'&Exit',self)
exitAct.setShortcut('Ctrl+Q') #快捷键
exitAct.setStatusTip('Exit Application')
exitAct.triggered.connect(qApp.quit) impMenu = QMenu("Import",self)
impAct = QAction("Import mail",self)
impMenu.addAction(impAct) self.statusBar() menubar = self.menuBar()
fileMenu = menubar.addMenu("&File")
fileMenu.addAction(exitAct) newAct = QAction("new",self)
fileMenu.addAction(newAct)
fileMenu.addMenu(impMenu) toolbar = self.addToolBar('Exit')
toolbar.addAction(exitAct)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('simple menu')
self.show() if __name__ == '__main__': app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

pyqt5 -——菜单和工具栏的更多相关文章

  1. pyqt5 菜单,工具栏,线程,matplotlib

    import sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QMainWindow, QMenuBar, QToolBar ...

  2. PyQt5教程——菜单和工具栏(3)

    PyQt5中的菜单和工具栏 在这部分的PyQt5教程中,我们将创建菜单和工具栏.菜单式位于菜单栏的一组命令操作.工具栏是应用窗体中由按钮和一些常规命令操作组成的组件. 主窗口 QMainWindow类 ...

  3. 在PyQt中构建 Python 菜单栏、菜单和工具栏

    摘要:菜单.工具栏和状态栏是大多数GUI 应用程序的常见且重要的图形组件.您可以使用它们为您的用户提供一种快速访问应用程序选项和功能的方法. 本文分享自华为云社区<Python 和 PyQt:创 ...

  4. WinForm 菜单和工具栏

    菜单和工具栏: 1.MenuStrip:顶部菜单 优先级最高,默认在最顶部 (1)分割线:a.打一个减号 “-”                   b.右键插入Separator (2)点击事件:每 ...

  5. WinForm菜单和工具栏

    菜单和工具栏: 1.MenuStrip - 顶部菜单栏分割线:1输入- 2.右键插入 |SpearTOR 快捷键设置:每一个项右键属性的最下面可以设置快捷键不管选项隐藏还是菜单隐藏,快捷键都管用 2. ...

  6. Windows系统:桌面,开始菜单和工具栏都不见了

    win7桌面,开始菜单和工具栏都不见了 ctrl+alt+del 打开任务管理器 然后文件-运行 ---  输入框里输入  explorer.exe 其实就是打开系统文件夹下的(大约是‘windows ...

  7. winform(多窗体、菜单和工具栏)

    一.多窗体 1.哪个是主窗体 利用From1的button将From2打开 private void button1_Click(object sender, EventArgs e) { Form2 ...

  8. WinFormd多窗体 菜单和工具栏:

    一.多窗体 1.哪个是主窗体 主窗体就是如果关掉这个窗体,程序就会退出的那个窗体1问题:主窗体隐藏了,关闭其它窗体后,没有将主窗体显示/关闭,那么程序就关不上了 办法:构造函数传值,将窗体传到另一个窗 ...

  9. WPF学习(二)布局与菜单、工具栏

    布局 //表格①Grid//3列 4行的表格   <Grid>    <Grid.ColumDefinitions>             <ColumnDefinti ...

随机推荐

  1. Python基础:四、python的优缺点

    python是一门动态解释性的强类型语言 python的优点: 1. python的定位是"优雅"."明确"."简单" python程序看上 ...

  2. 记SCOI2019

    离精英体验营结束已两周的,要哭的要笑的现在也一定释怀了.是时候冷静分析一下这次的考试了.时间序虽然有流水账的嫌疑,但这毕竟是OI界的流行风气. day0 早上坐学校包的商务车去了电子科技大学.走在来过 ...

  3. asp.net core webapi处理Post请求中的request payload

    request payload的Content-Type实际上是text/plain的,如果请求的 Content-Type 为 application/json,这将导致415 Unsupporte ...

  4. sql server数据库入门

    create database 学生信息 on primary  //建立在主文件文件组 ( name='学生信息_data', filename='D:\2011上半年度\数据库\sql代码\xue ...

  5. Http长连接

    1.Http长连接 Http的请求时在TCP连接上进行发送的,TCP的连接分为长连接和短连接 打开www.baidu.com,查看Connection ID 如下图. Connection ID代表T ...

  6. .NET 使用 Azure Blob 存储图片或文件

    使用的是VS2017 一.先使用 NuGet 获取这两个包. 执行以下步骤: 在“解决方案资源管理器”中,右键单击你的项目并选择“管理 NuGet 包”. 1.在线搜索“WindowsAzure.St ...

  7. HttpSimpleClient连接服务器

    public class HttpSimpleClient { /** * 发送GET请求. */ static public HttpResult httpGet(String url, List& ...

  8. MariaDB的安装与启动

    MariaDB的安装与启动 1.安装前需要删除系统已存在的mysql及mariadb [root@vm172--- ~]# rpm -qa|grep mysql [root@vm172--- ~]# ...

  9. HTML5 script 标签的 crossorigin 和integrity属性的作用

    Bootstrap 4 依赖的基础库中出现了两个新的属性 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.m ...

  10. 2018-2019-2 20175311 实验二 《Java开发环境的熟悉》实验报告

    2018-2019-2 20175303 实验二 <Java开发环境的熟悉>实验报告 一.实验准备 1.了解掌握实验所要用到的三种代码 伪代码 产品代码 测试代码 2.IDEA中配置单元测 ...