前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PYQT中的菜单和工具栏

PYQT中的菜单和工具栏

作者头像
小飞侠xp
发布2018-12-24 10:27:35
2.5K0
发布2018-12-24 10:27:35
举报

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

QMainWindow类提供了一个应用主窗口。默认创建一个拥有状态栏、工具栏和菜单栏的经典应用窗口骨架。

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组件帮助创建完成(依赖于QMainWindow组件)。 self.statusBar().showMessage('Ready') 为了得到状态栏,我们调用了QtGui.QMainWindow类的statusBar()方法。第一次调用这个方法创建了一个状态栏。随后方法返回状态栏对象。然后用showMessage()方法在状态栏上显示一些信息。

菜单栏

菜单栏是GUI应用的常规组成部分。是位于各种菜单中的一组命令操作(Mac OS 对待菜单栏有些不同。为了获得全平台一致的效果,我们可以在代码中加入一行:menubar.setNativeMenuBar(False)

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon
class 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.show()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_()) 

在上面的例子中,我们创建了有一个菜单项的菜单栏。这个菜单项包含一个选中后中断应用的动作。

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

QAction是一个用于菜单栏、工具栏或自定义快捷键的抽象动作行为。在上面的三行中,我们创建了一个有指定图标和文本为'Exit'的标签。另外,还为这个动作定义了一个快捷键。第三行创建一个当我们鼠标浮于菜单项之上就会显示的一个状态提示。

当我们选中特定的动作,一个触发信号会被发射。信号连接到QApplication组件的quit()方法。这样就中断了应用。

exitAction.triggered.connect(qApp.quit)

menuBar()方法创建了一个菜单栏。我们创建一个file菜单,然后将退出动作添加到file菜单中。

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

菜单可以集成所有命令,这样我们可以在应用中使用这些被集成的命令。工具栏提供了一个快速访问常用命令的方式

# !/usr/bin/python3

# -*- coding: utf-8 -*-
"""
This program creates a toolbar.
The toolbar has one action, which
terminates the application, if triggered.
"""
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        exitAction = QAction(QIcon('exit24.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.show()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
子菜单

子菜单是位于另一个菜单中的菜单。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
This program creates a submenu.
"""

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QMenu, QApplication

class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):         
        
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')
        
        impMenu = QMenu('Import', self)
        impAct = QAction('Import mail', self) 
        impMenu.addAction(impAct)
        
        newAct = QAction('New', self)        
        
        fileMenu.addAction(newAct)
        fileMenu.addMenu(impMenu)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Submenu')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

在示例中,我们有两个菜单项; 一个位于“文件”菜单中,另一个位于“文件”的“导入”子菜单中。

  1. 使用创建新菜单QMenu。
impMenu = QMenu('Import', self)
  1. 将一个动作添加到子菜单中addAction()
impAct = QAction('Import mail', self) 
impMenu.addAction(impAct)
Context menu(弹出菜单)

上下文菜单(也称为弹出菜单)是在某些上下文下显示的命令列表。例如,在Opera网页浏览器中,当我们右键单击网页时,我们会得到一个上下文菜单。在这里,我们可以重新加载页面,返回或查看页面源。如果我们右键单击工具栏,我们将获得另一个用于管理工具栏的上下文菜单。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a context menu.

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017
"""

import sys
from PyQt5.QtWidgets import QMainWindow, qApp, QMenu, QApplication

class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):         
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Context menu')    
        self.show()
    
    
    def contextMenuEvent(self, event):
       
           cmenu = QMenu(self)
           
           newAct = cmenu.addAction("New")
           opnAct = cmenu.addAction("Open")
           quitAct = cmenu.addAction("Quit")
           action = cmenu.exec_(self.mapToGlobal(event.pos()))
           
           if action == quitAct:
               qApp.quit()
       
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
工具栏

菜单可以集成所有命令,这样我们可以在应用中使用这些被集成的命令。工具栏提供了一个快速访问常用命令的方式

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This program creates a toolbar.
The toolbar has one action, which
terminates the application, if triggered.

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017
"""

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon

class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):               
        
        exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.triggered.connect(qApp.quit)
        
        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAct)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Toolbar')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

上述例子中,我们创建了一个简单的工具栏。工具栏有一个动作,当这个退出动作被触发时应用将会被中断。

  1. 我们创建了一个动作对象,和之前菜单栏中的部分代码相似。这个动作有一个标签,图标和快捷键。并且将QtGui.QMainWindow的quit()方法连接到了触发信号上。
exitAction = QAction(QIcon('exit24.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(qApp.quit)
  1. 这里我们创建了一个工具栏,并且在其中插入一个动作对象。
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAction)
组合在一起
#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
This program creates a skeleton of
a classic GUI application with a menubar,
toolbar, statusbar, and a central widget. 
"""

import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication
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('exit24.png'), 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(self.close)
        self.statusBar()
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAct)
        toolbar = self.addToolBar('Exit')
        toolbar.addAction(exitAct)       
        self.setGeometry(300, 300, 350, 250)
        self.setWindowTitle('Main window')    
        self.show()             
if __name__ == '__main__':    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

事例代码创建了一个带有菜单栏、工具栏和状态栏的经典GUI应用骨架。

  1. 在这里我们创建了一个文本编辑框组件。我们将它设置成QMainWindow的中心组件。中心组件占据了所有剩下的空间。
textEdit = QTextEdit()
self.setCentralWidget(textEdit)

mainwindow

在这个部分的PyQt5中,我们使用了菜单、工具栏、状态栏和一个应用主窗口。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.12.05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 菜单栏
  • 工具栏
  • 子菜单
  • Context menu(弹出菜单)
  • 工具栏
  • 组合在一起
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档