A menubar is a common part of a GUI application. It is a group of commands located in various menus.

#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial This program creates a menubar. The
menubar has one menu with an exit action. author: Jan Bodnar
website: zetcode.com
last edited: August 2011
""" import sys
from PyQt4 import QtGui class Example(QtGui.QMainWindow): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.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() def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

In the above example, we create a menubar with one menu. This menu contains one action which terminates the application if selected. A statusbar is created as well. The action is accessible with theCtrl+Q shortcut.

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

QtGui.QAction is an abstraction for actions performed with a menubar, toolbar or with a custom keyboard shortcut. In the above three lines, we create an action with a specific icon and an 'Exit' label. Furthermore, a shortcut is defined for this action. The third line creates a status tip which is shown in the statusbar when we hover a mouse pointer over the menu item.

exitAction.triggered.connect(QtGui.qApp.quit)

When we select this particular action, a triggered signal is emitted. The signal is connected to thequit() method of the QtGui.QApplication widget. This terminates the application.

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

The menuBar() method creates a menubar. We create a file menu and append the exit action to it.

Menubar的更多相关文章

  1. 【Windows编程】系列第七篇:Menubar的创建和使用

    上一篇我们学习了利用windows API创建工具栏和菜单栏,与上一篇紧密联系的就是菜单栏,菜单栏是一个大多数复杂一些的Windows应用程序不可或缺的部分.比如下图就是Windows自带的记事本的菜 ...

  2. “菜单”(menubar)和“工具栏”(toolbars)

    "菜单"(menubar)和"工具栏"(toolbars) "菜单" (menubar)和"工具栏"(toolbars) ...

  3. flashbuilder mx组件 MenuBar

    来源:http://www.cuplayer.com/player/PlayerCode/Flex/2013/0118661.html <fx:Script> <![CDATA[ i ...

  4. 3.关于QT中的MainWindow窗口,MenuBar,ToolBar,QuickTip等方面的知识点

     1 新建一个空Qt项目 编写12MainWindow.pro HEADERS += \ MyMainWindow.h \ MyView.h SOURCES += \ MyMainWindow.c ...

  5. WorldWind源码剖析系列:图层管理器按钮类LayerManagerButton和菜单条类MenuBar

    WorldWindow用户定制控件类中所包含的的可视化子控件主要有:图层管理器按钮类LayerManagerButton和菜单条类MenuBar.BmngLoader类中所包含的的可视化子控件主要有: ...

  6. Texas Instruments matrix-gui-2.0 hacking -- menubar.php

    <?php /* * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ * * * Redistrib ...

  7. Statusbar、Menubar、Toolbar合集

    In the last example of this section, we create a menubar, a toolbar and a statusbar. We also create ...

  8. Flex4以后Menubar背景设置问题

    flex4的 backgroundColor失效,需要使用 contentBackgroundColor menubar.setStyle("contentBackgroundColor&q ...

  9. 零元学Expression Blend 4 - Chapter 44 Flash做的到的Blend也可以!轻松制作拥有动画的MenuBar!(下)

    原文:零元学Expression Blend 4 - Chapter 44 Flash做的到的Blend也可以!轻松制作拥有动画的MenuBar!(下) 抱歉久等了!!!! 终於到了动画MenuBar ...

随机推荐

  1. centos7安装maven

    下载maven 下载地址:http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3. ...

  2. 关于php一些问题

    为什么说php是弱语言? 本身不严格区分变量的类型. 为什么说php是动态语言? 程序在运行时可以改变其结构.所谓的动态类型语言,意思就是类型的检查是在运行时做的. 为什么说php是脚本语言? 不需要 ...

  3. 解决 git push Failed to connect to 127.0.0.1 port 45463: 拒绝连接

    使用Github pull 代码突然报错: Failed to connect to 127.0.0.1 port 43421: Connection refused 使用 lsof 发现端口未被占用 ...

  4. Jenkins搭建.NET自动编译测试与发布环境

    本文地址: http://blog.csdn.net/wangjia184/article/details/18365553 操作系统Windows, 确保需要的.NET Framework已经安装 ...

  5. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  6. 异常:javax.el.PropertyNotFoundException: Property 'id' not found on ..........

    在后台action中查询数据库返回的UserInfo对象后 将uDisplayName传递到jsp中,jsp中用${userInfo.uDisplayName}接收值时报错: 异常:javax.el. ...

  7. 将MyEclipse项目导入到Eclipse中

    1.请首先确保你的eclipse是javaee版本的,或者已经安装wtp插件 2.然后修改eclipse工程下的.project文件: 3.在<natures></natures&g ...

  8. [Swift] Swift3.0--GCD

    reference to : http://www.jianshu.com/p/4c983388dca6 估计现在好多人在为这一块头疼,所以先来点干货. //最常用模板 //全局队列异步执行 Disp ...

  9. Expectation Maximization-EM(期望最大化)-算法以及源码

    在统计计算中,最大期望(EM)算法是在概率(probabilistic)模型中寻找参数最大似然估计的算法,其中概率模型依赖于无法观测的隐藏变量(Latent Variable).最大期望经常用在机器学 ...

  10. 解决 Ubuntu Software (Software Center) Crash 问题

    问题描述: no application data found 解决方式: sudo apt purge gnome-software ubuntu-software sudo apt autorem ...