Closing a window
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的更多相关文章
- Sublime Text 2配置文件详解
Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...
- vim - buffer
1. buffer switching http://vim.wikia.com/wiki/Easier_buffer_switching :buffer:ls:files 2. vim defaul ...
- SUBLIME TEXT 2 设置文件详解
SUBLIME TEXT 2 设置文件详解 Preferences.sublime-settings文件: // While you can edit this file, it’s best to ...
- js关闭浏览器的tab页(兼容)
由于在脚本中使用了 window.close(), 当前非弹出窗口在最新版本的chrome和firefox里总是不能关闭,而在 IE中是可以关闭的 . 在console中弹出提示"Scrip ...
- NUnit-Console 命令行选项详解
本文为 Dennis Gao 原创或翻译技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. NUnit-Console 命令行选项 NUnit-Console 命令行选项列表 指定运行哪 ...
- [转]Sublime Text 2 设置文件详解
Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...
- Sublime Text 设置文件详解
Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自 ...
- Oracle Database 11g express edition
commands : show sys connect sys as sysdba or connect system as sysdba logout or disc clear screen or ...
- 在vs环境中跑动sift特征提取(代码部分)
因为在前两天的学习中发现.在opencv环境中跑动sift特征点提取还是比较困难的. 所以在此,进行记述. 遇到的问题分别有,csdn不愿意花费积分.配置gtk困难.教程海量然而能跑者鲜.描述不详尽等 ...
随机推荐
- .NET程序员提高效率的70多个开发工具
工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存管理,诊断工具等,涉及开发过程的各个环节, ...
- PostgreSQL修改数据库目录/数据库目录迁移
说明:以9+版本为例,10+的版本只要把目录替换一下即可.迁移目录肯定是要停服的! 1.在数据库软件安装之后,初始化数据库时候,可以指定初始化时创建的数据库的默认文件路径 /usr/local/pgs ...
- mysql slock
http://www.itdks.com/dakashuo/new/dakalive/detail/3888
- DM6446开发攻略:UBOOT-2009.03移植及nand flash烧写
有关DAVINCI U-BOOT的移植,以前写过一篇u-boot-1.3.4(2008年的),其实和这个u-boot-2009.03差别不大,只不过这个u-boot-2009.03是从TI的网站上下载 ...
- ubuntu C++开发环境
最近在VM中装了Ubuntu,为了开发程序,于是在网上找了些由于C/C++开发环境搭建的资料,供大家参考. 以下文字主要讲如何搭建Code::Blocks+wxWidgets. 搭建步骤: 1.安装编 ...
- java静态初始化数据
1.通过静态成员变量和静态方法组合(比较单一) public class A{ private static String t=getInit(); private static String get ...
- OpenCV学习(36) 人脸识别(1)
本文主要参考OpenCV人脸识别教程:http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html 1.OpenCV ...
- Informatica 常用组件Lookup之六 查询
PowerCenter 基于您在查找转换中配置的端口和属性来查询查找.当第一行输入到查找转换时,PowerCenter 运行一个默认的 SQL 语句.如果使用关系查找,您可以在"查找 SQL ...
- Word Break leetcode java
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- C#中图片切割,图片压缩,缩略图生成的代码
**//// <summary> /// 图片切割函数 /// </summary> /// <param name="sourceFile"> ...