#!/usr/bin/env python

 import sys
from PyQt5.QtWidgets import (QApplication,
QMainWindow,
QWidget,
QAction,
QLabel,
QTextEdit,
QLineEdit,
QPushButton,
QGridLayout)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent, Qt.Window) # 创建一个菜单栏:
bar_menu = self.menuBar()
# 为菜单栏添加文件菜单
menu_file = bar_menu.addMenu('文件(&F)')
# 为菜单栏添加编辑菜单
menu_edit = bar_menu.addMenu('编辑(&E)') # 添加一个动作:
action_file = QAction('打开', self)
# 为动作添加快捷建:
# 值得注意的是在 MAC os 中 Ctrl 指的是 command 。
action_file.setShortcut('Ctrl+O')
# 为动作添加图标:
action_file.setIcon(QIcon('open.png'))
# 将点击动作的信号连接到 action_open 方法:
action_file.triggered.connect(self.action_open)
# 将打开动作添加到文件菜单中:
menu_file.addAction(action_file) action_copy = QAction('复制', self)
action_copy.setIcon(QIcon('copy.png'))
# 在 MAC os 中 Meta 才是 Ctrl 按钮:
action_copy.setShortcut('Meta+C')
action_copy.triggered.connect(self.action_copy)
menu_edit.addAction(action_copy) # 创建一个工具栏:
bar_tool = self.addToolBar('工具栏')
# 为工具栏添加按钮:
bar_tool.addAction(action_file)
# 为添加分割线:
bar_tool.addSeparator()
bar_tool.addAction(action_copy) label_1 = QLabel('居右')
# 设置Label的文字为居右并垂直居中:
label_1.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
label_2 = QLabel('宽度因子为 1')
label_3 = QLabel('居中')
# 设置Label的文字为居中并垂直居中:
label_3.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
self.label_4 = QLabel('Label_4') # 创建一个状态栏:
status = self.statusBar()
# 在状态栏显示信息:
# 注意:显示信息时,组件不会显示
# status.showMessage("Ready!")
# 为状态栏添加一个stretch(拉伸因子)为1的Label
status.addWidget(label_1, 1)
status.addWidget(label_2, 1)
status.addWidget(label_3, 3)
status.addWidget(self.label_4, 1) """
为 QMainWindow 添加其他组件的方法:
1. 将组件添加到一个布局中;
2. 创建一个 QWidget 设置为 1 中的布局;
3. 将 2 中的 QWidget 设为central widget 。
""" line_edit = QLineEdit()
# 为 line_edit 设置灰色提示文字:
line_edit.setPlaceholderText('请输入...') push_button = QPushButton()
push_button.setText('确认') text_edit = QTextEdit()
text_edit.setPlaceholderText("编辑结果...") # 创建一个网格布局:
layout_grid = QGridLayout()
layout_grid.addWidget(line_edit, 0, 0)
layout_grid.addWidget(push_button, 0, 1)
layout_grid.addWidget(text_edit, 1, 0, 2, 2) # 创建一个 QWidget ,并将其布局设置为 layout_grid :
widget = QWidget()
widget.setLayout(layout_grid)
# 将 widget 设为主窗口的 central widget :
self.setCentralWidget(widget) # 设置窗口大小:
self.resize(500, 500)
# 设置窗口标题:
self.setWindowTitle(u"QMainWindow")
# 显示窗口:
self.show() def action_open(self):
self.label_4.setText("按下了打开按钮!") def action_copy(self):
self.label_4.setText("按下了复制按钮!") if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())

效果如下图:

【PyQt5 学习记录】005:QMainWindow 及状态栏、菜单栏和工具栏的更多相关文章

  1. 【PyQt5 学习记录】006:重写窗口事件及QMessageBox

    #!/usr/bin/env python import sys from PyQt5.QtWidgets import (QApplication, QMainWindow, QMessageBox ...

  2. 【PyQt5 学习记录】011:使用 QListWidet 创建列表

    使用 QListWidet 创建列表 作者: 八月未见 博客: https://www.cnblogs.com/jmtm/ 创建列表: list_widget = QListWidget() 插入项目 ...

  3. 【PyQt5 学习记录】010:QSplitter

    import sys from PyQt5.QtWidgets import (QApplication, QWidget, QSplitter, QTextEdit, QPushButton, QL ...

  4. 【PyQt5 学习记录】008:改变窗口样式之二

    super(MainWindow, self).__init__(parent, flags=Qt.Widget) 首先需要关闭窗口的默认样式,以方便定时想要的样式: self.setWindowFl ...

  5. 【PyQt5 学习记录】007:改变窗口样式之一

    class MainWindow(QMainWindow): 2 def __init__(self, parent=None): 3 super(MainWindow, self).__init__ ...

  6. 【PyQt5 学习记录】004:简单QThread笔记

    在文本编辑框中每隔几秒添加一行文本,代码如下: #!/usr/bin/python3 # -*- coding:utf-8 -*- import sys from PyQt5.QtWidgets im ...

  7. 【PyQt5 学习记录】001:第一个界面

    PyQt5 的安装很简单: pip3 install PyQt5 然后就是写一个最简单的界面试试手: import sys from PyQt5.QtWidgets import QApplicati ...

  8. 【PyQt5 学习记录】009:批量创建组件并查找

    这里需要在创建组件时使用 setObjectName() 为其添加 objectName 属性,再通过 findChild() 或者 findChildren() 函数来查找. 举个栗子,批量创建10 ...

  9. 【PyQt5 学习记录】003:水平布局和获取屏幕尺寸

    #!/usr/bin/python3 # -*- coding:utf-8 -*- import sys from PySide2.QtWidgets import (QApplication, QW ...

随机推荐

  1. [Swift实际操作]七、常见概念-(1).范围Range、ClosedRange和NSRange的使用实际操作

    本文将为你解析区间的概念.在对字符串进行处理时,经常会使用到区间.例如获得字符串中从某个位置开始到另一个位置之间的内容. 首先导入需要使用到的框架 import Foundation 区间分为闭区间和 ...

  2. class字节码结构(二)(访问标志、类索引、父类索引、接口索引集合)

    <Java虚拟机原理图解>1.3.class文件中的访问标志.类索引.父类索引.接口索引集合 字节码总体结构: 访问标志(access_flags)能够表示什么? 访问标志(access_ ...

  3. SVN图形客户端上传静态库.a文件失败

    1.原因客户端未添加静态库 2.解决办法 到项目静态库所在目录用命令行添加静态库文件 svn add ****.a 3.可能存在问题 Mac OS的自带SVN版本过低不能添加,报错如下: svn: E ...

  4. SpringBoot学习笔记(一)基础

    Spring Boot理念 习惯优于配置.使用Spring Boot很容易创建一个独立运行(运行jar,内嵌servlet容器).准生产级别的基于Spring框架的项目,使用SpringBoot可以不 ...

  5. Java_使用日志

    日志有什么用? 在实际开发中,不可能使用控制台输出所有数据,可以用日志把程序运行的过程记录下来,包括运行中出现的异常和BUG 当出现问题的时候,程序员可以去查看日志,从而能快速的找到问题所在. 一般来 ...

  6. ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

    Struts2未配置Log4j2.xml报错 Log4j2.xml中的配置 log4j的jar包:log4j-core-2.7.jar log4j2只支持xml和json两种格式的配置,所以配置log ...

  7. POJ 2247

    #include<iostream> #include<algorithm> #include<vector> #include<string> #in ...

  8. 从Windows下引导安装ubuntu

    Ubuntu引导安装 首先是安装前的准备工作 1.需要安装的 ubuntu系统的ISO镜像. 2.easyBCD引导软件 3.所在的Windows系统(本人win7下安装) 4.DiskGenius( ...

  9. Storm原理及安装

    http://my.oschina.net/leejun2005/blog/147607 http://www.storm-geek.com/forum.php http://www.zhangjih ...

  10. SocketIo+SpringMvc实现文件的上传下载

    SocketIo+SpringMvc实现文件的上传下载 socketIo不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示: GIT地址 ...