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. curl多文件上传

    发送: header('Content-type:text/html; charset=utf-8'); //声明编码//模拟批量POST上传文件$url = 'http://test.cm/rece ...

  2. ARM ® and Thumb ®-2 指令系统

    指令表关键词        Rm {, <opsh>} 寄存器移位方式,将寄存器的移位结果作为操作数而Rm值保持不变       <Operand2> 灵活的使用第二个操作数. ...

  3. 达芬奇TI DVSDK之视频数据流过程分析

    作者:openwince@gmail.com 博客:http://www.cnblogs.com/tinz    本文的copyright归openwince@gmail.com所有,使用GPL发布, ...

  4. 点赞和吐糟Adblock Plus~进阶教程

    前言:Adblock Plus后文都简称ABP,这是一篇ABP进阶教程!用ABP实现flashBlock和NoScript.推荐有相当基础的阅读.刚開始学习的人先看懂这里:http://adblock ...

  5. 如何让两个div并排,并且div要看得见边框

    <div style="float:left; width:100px; height:100px; border:2px solid #0000FF;"></d ...

  6. Android布局优化之ViewStub、include、merge使用与源码分析

    在开发中UI布局是我们都会遇到的问题,随着UI越来越多,布局的重复性.复杂度也会随之增长.Android官方给了几个优化的方法,但是网络上的资料基本上都是对官方资料的翻译,这些资料都特别的简单,经常会 ...

  7. JAVA常见算法题(三十二)---找规律

    题目一: 4,5,15,45,135,405,__ 题目二: 524,244,954,674,394,15,725, __ 题目三: 7,8,6,9,10,7,4,4,5,__ 求横线位置的整数. * ...

  8. cout的输出格式初探

    在C++中,cout代表的是标准输出设备,即显示器,相对于C语言中所使用的printf函数,cout显得更为灵活.下面以30.300和1024三个数为例子,简单说明cout输出时所选格式的输出.cou ...

  9. XSS第四节,XSS攻击实例(一)

    在开始实例的讲解之前,先看一下XSS的危害情况,第一张图中说明和XSS相关的CVE漏洞有7417个(http://web.nvd.nist.gov/view/vuln/search-results?q ...

  10. iOS开发-CoreMotion框架(加速计和陀螺仪)

    CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速度计和陀螺仪,在iOS4之前加速度计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来 ...