PyQt5学习记录(2)---QMainWindow菜单栏、状态栏和工具栏

QMainWindow

QMainWindow是继承自QWidget,提供一个应用程序的主窗口。通过QMainWindow你可以添加自己的QToolBarsQDockWidgetsQMenuBarQStatusBar等。见下图:

所以如果我们写程序的话,主窗口一般都是继承QMainWindow,这个有点类似android里的Activity的意思。接下来我们就对最下面的状态栏(StatusBar)、最上面的菜单栏(MenuBar)、以及工具栏(ToolBars)做单独的说明.

状态栏(StatusBar)

源码如下:

#coding=utf-8
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication

class Example1(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
        pass

    def initUI(self):
        self.statusBar().showMessage('Reday')
        self.setGeometry(300, 300, 450, 450)
        self.setWindowTitle('QMainWindow的状态栏demo')
        self.show()
        pass

def main1():
    app = QApplication(sys.argv)
    example = Example1()
    sys.exit(app.exec_())
    pass

if __name__ == '__main__':
    main1()

效果如下所示:

核心提示:通过statusBar()让QMainWindow创建一个状态栏,然后showMessage打印信息,之后再调statusBar将直接返回这个状态栏对象.

菜单栏(MenuBar)

一个应用程序可以有很多个菜单(横向的),每个菜单里有很多个Action(竖排的)。
源码如下:

#coding=utf-8
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QMenuBar, QAction, qApp
from PyQt5.QtGui import QIcon

# 菜单栏
class Example2(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
        pass

    def initUI(self):
        self.statusBar() #创建一个空的状态栏
        menubar = self.menuBar()
        menubar.setNativeMenuBar(False)
        fileMenu = menubar.addMenu('File')

        #给menu创建一个Action
        exitAction = QAction(QIcon('exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctr+Q')
        exitAction.setStatusTip('Exit Application')
        exitAction.triggered.connect(qApp.quit)

        #将这个Action添加到fileMenu上
        fileMenu.addAction(exitAction)

        self.setWindowTitle('QMainWindow的菜单栏的显示')
        self.setGeometry(300, 300, 450, 450)
        self.show()
        pass

def main2():
    app = QApplication(sys.argv)
    example = Example2()
    sys.exit(app.exec_())
    pass

if __name__ == '__main__':
    main2()

运行效果:

要点解释

  1. 在Mac上要加menubar.setNativeMenuBar(False),否则很可能不显示。这是因为Mac上的菜单栏不在应用程序的顶部,而是在整个桌面的顶部。加上这句话后显示的是Windows上的样式。
  2. 一个Menu必须要有一个Action才会显示,至少在Mac上是这样。
  3. Menu的关键字FileView,系统会自动给你加一些Action。
  4. Action的关键字为Exit时,同样不显示。下面是Mac上把menubar.setNativeMenuBar(False)去掉,把Action的关键字改成其他的效果.
  5. exitAction.triggered.connect(qApp.quit)的效果和exitAction.triggered.connect(QCoreApplication.instance().exit)效果是一样的,可以看qApp实际是一个宏。
  6. Action的信号是triggered, QPushButton的信号是clicked
  7. 在Mac上设置的退出快捷键好像没有效果。

工具栏ToolBar

在菜单栏里可以把所有的按钮都集中进来,但是最常用的按钮可以放在工具栏,即菜单栏的下方。

#coding=utf-8
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QMenuBar, QAction, qApp
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QCoreApplication

class Example3(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        exitAction = QAction(QIcon('exit.png'), '退出吧宝宝', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)

        self.toolbar = self.addToolBar('Exit123')
        self.toolbar.addAction(exitAction)
        self.toolbar.addAction(QIcon('exit.png'), '翻滚')

        self.setGeometry(300, 300, 450, 450)
        self.setWindowTitle('QMainWindow的ToolBar')
        self.show()
        pass

def main3():
    app = QApplication(sys.argv)
    example = Example3()
    sys.exit(app.exec_())
    pass

if __name__ == '__main__':
    main3()

效果如下:

要点解释

  1. 通过self.addToolBar('Exit')创建一个ToolBar,一个ToolBar可以加多个Action,默认是横向排列的。这里的Exit仅仅是标识TooBar,因为一个QMainWindow也可以有多个ToolBar。如下代码展示:
    self.toolbar = self.addToolBar('Exit123')
        self.toolbar.addAction(exitAction)
        self.toolbar.addAction(QIcon('exit.png'), '翻滚')

        self.toolbar2 = self.addToolBar('Exit456')
        self.toolbar2.addAction(exitAction)
        self.toolbar2.addAction(QIcon('exit.png'), '翻滚')

效果如下:

状态栏、工具栏、菜单栏综合练习

代码如下:

#coding=utf-8
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QMenuBar, QAction, qApp, QTextEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QCoreApplication

class Example4(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        textEdit = QTextEdit()
        self.setCentralWidget(textEdit)

        # 创建一个Action
        exitAction = QAction(QIcon('exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('退出程序')
        exitAction.triggered.connect(self.close)

        # 底部状态栏
        self.statusBar().showMessage('状态栏看我')

        # 顶部菜单栏
        menubar = self.menuBar()
        menubar.setNativeMenuBar(False)
        fileMenu = menubar.addMenu('File')
        fileMenu.addAction(exitAction)

        # 次顶部的工具栏
        toolbar = self.addToolBar('Exit')
        toolbar.addAction(exitAction)

        self.setGeometry(300, 300, 500, 500)
        self.setWindowTitle('QMainWindow综合使用')
        self.show()
        pass

def main4():
    app = QApplication(sys.argv)
    example = Example4()
    sys.exit(app.exec_())
    pass

if __name__ == '__main__':
    main4()

效果

要点提示:setStatusTip设置的东西会在底部的状态栏显示。

--------------------- 本文来自 yanzi1225627 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yanzi1225627/article/details/72598840?utm_source=copy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt5中,窗口可以分为三类:QMainWindow、QWidget和QDialog。QMainWindow是最常见的窗口形式,可以包含菜单栏工具栏状态栏、标题栏等,通常用于创建GUI程序的主窗口。QWidget是窗口的基类,可以直接使用或继承后再使用,它没有特定的功能和布局。QDialog是对话框窗口的基类,主要用于执行短期任务或与用户进行互动。与QMainWindow不同,QDialog窗口不包含菜单栏工具栏状态栏等组件。根据需求选择合适的窗口类型,可以更方便地设计和开发PyQt5应用程序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [PyQt5 GUI编程——窗口类型](https://blog.csdn.net/L13682726202/article/details/92024539)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Python快速入门系列:PyQt5 快速开发GUI-窗口类型以及主窗口创建](https://blog.csdn.net/weixin_39975261/article/details/112011679)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值