The obvious way to how to close a window is to click on the x mark on the titlebar. In the next example, we will show how we can programatically close our window. We will briefly touch signals and slots.

The following is the constructor of a QtGui.QPushButton that we will use in our example.

QPushButton(string text, QWidget parent = None)

The text parameter is a text that will be displayed on the button. The parent is a widget on which we place our button. In our case it will be a QtGui.QWidget.

#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial This program creates a quit
button. When we press the button,
the application terminates. author: Jan Bodnar
website: zetcode.com
last edited: October 2011
""" import sys
from PyQt4 import QtGui, QtCore class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): qbtn = QtGui.QPushButton('Quit', self)
qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(50, 50) self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Quit button')
self.show() def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

In this example, we create a quit button. Upon clicking on the button, the application terminates.

from PyQt4 import QtGui, QtCore

An object from the QtCore module will be needed. Therefore, we import the module.

qbtn = QtGui.QPushButton('Quit', self)

We create a push button. The button is an instance of the QtGui.QPushButton class. The first parameter of the constructor is the label of the button. The second parameter is the parent widget. The parent widget is the Example widget, which is a QtGui.QWidget by inheritance.

qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)

The event processing system in PyQt4 is built with the signal & slot mechanism. If we click on the button, the signal clicked is emitted. The slot can be a Qt slot or any Python callable. TheQtCore.QCoreApplication contains the main event loop. It processes and dispatches all events. Theinstance() method gives us its current instance. Note that QtCore.QCoreApplication is created with theQtGui.QApplication. The clicked signal is connected to the quit() method which terminates the application. The communication is done between two objects: the sender and the receiver. The sender is the push button, the receiver is the application object.

Figure: Quit button

Closing a window的更多相关文章

  1. Sublime Text 2配置文件详解

    Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...

  2. vim - buffer

    1. buffer switching http://vim.wikia.com/wiki/Easier_buffer_switching :buffer:ls:files 2. vim defaul ...

  3. SUBLIME TEXT 2 设置文件详解

    SUBLIME TEXT 2 设置文件详解 Preferences.sublime-settings文件: // While you can edit this file, it’s best to ...

  4. js关闭浏览器的tab页(兼容)

    由于在脚本中使用了 window.close(), 当前非弹出窗口在最新版本的chrome和firefox里总是不能关闭,而在 IE中是可以关闭的 . 在console中弹出提示"Scrip ...

  5. NUnit-Console 命令行选项详解

    本文为 Dennis Gao 原创或翻译技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. NUnit-Console 命令行选项 NUnit-Console 命令行选项列表 指定运行哪 ...

  6. [转]Sublime Text 2 设置文件详解

    Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...

  7. Sublime Text 设置文件详解

     Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自 ...

  8. Oracle Database 11g express edition

    commands : show sys connect sys as sysdba or connect system as sysdba logout or disc clear screen or ...

  9. 在vs环境中跑动sift特征提取(代码部分)

    因为在前两天的学习中发现.在opencv环境中跑动sift特征点提取还是比较困难的. 所以在此,进行记述. 遇到的问题分别有,csdn不愿意花费积分.配置gtk困难.教程海量然而能跑者鲜.描述不详尽等 ...

随机推荐

  1. poj 1279 Art Gallery - 求多边形核的面积

    /* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...

  2. if....else的基本用法

    if....else...是基本流程控制语句 1.基本格式: if(条件){ }else if(条件){ }else if(条件){ } ........ else{ } 解释:其中else if.e ...

  3. 【转载】C#堆和栈的区别

    原文出处 理解堆与栈对于理解.NET中的内存管理.垃圾回收.错误和异常.调试与日志有很大的帮助.垃圾回收的机制使程序员从复杂的内存管理中解脱出来,虽然绝大多数的C#程序并不需要程序员手动管理内存,但这 ...

  4. python接口自动化27-urlencode编码%E6%82%A0%E6%82%A0与解码

    前言 urllib.parse 里面三个方法:urlencode,quote,unquote详解. 在做接口自动化过程中,http协议在发送url的时候,是以urlencode的编码格式传过去的,通常 ...

  5. 修改后无警告全面支持non-ARC以及ARC的OpenUDID

    OpenUDID Open source initiative for a universal and persistent UDID solution for iOS. 首创的给iOS提供设备唯一标 ...

  6. iOS7以下设备获取mac地址

    注意,是iOS7以下的设备,直接上源码,获取mac地址都是为了唯一标识一个设备的,但iOS7设备的mac地址为 020000000000 MacAddress.h #include <sys/s ...

  7. navicat for mysql中添加注释

    mysql; # 这注释持续到行尾 mysql; -- 这注释持续到行尾 mysql ; mysql+ /* 这是 多行注释 */

  8. python3 urllib.request 网络请求操作

    python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...

  9. MySql_34道经典Sql试题

    MySql_34道经典Sql试题   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xiaouncle/article/details/799390 ...

  10. 【ContestHunter】【弱省胡策】【Round4】

    01分数规划(网络流)+状压DP+树形DP 官方题解地址:http://pan.baidu.com/s/1mg5S5z6 A 好神啊= =第一次写01分数规划 其实分数规划是要求$$ Maximize ...