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

时间: 2024-11-08 09:05:55

Closing a window的相关文章

TCP Silly Window Syndrome

In the topic describing TCP's Maximum Segment Size (MSS) parameter, I explained the trade-off in determining the optimal size of TCP segments. If segments are too large, we risk having them become fragmented at the IP level. Too small, and we get gre

window.close()提示 "script may close only the windows that were opened by it"

由于在脚本中使用了 window.close(), 当前非弹出窗口在最新版本的chrome和firefox里总是不能关闭,而在 IE中是可以关闭的 .在console中弹出提示"script may close only the windows that were opened by it" (脚本只能关闭它所打开的窗口), 不明白是什么原因. 研究了好几天.终于明白了问题所在. 首先,什么是非弹出窗口呢? 非弹出窗口,即是指(opener=null及非window.open()打开的

Sublime Text 2 配置手册

Preferences.sublime-settings文件: // While you can edit this file, it’s best to put your changes in // “User/Preferences.sublime-settings”, which overrides the settings in here. // // Settings may also be placed in file type specific options files, for

Sublime text3 设置的中文翻译

// While you can edit this file, it's best to put your changes in // "User/Preferences.sublime-settings", which overrides the settings in here. // // Settings may also be placed in file type specific options files, for // example, in Packages/Py

在InternetExplorer.Application中显示本地图片

忘记了,喜欢一个人的感觉 Demon's Blog  ?  程序设计  ?  在InternetExplorer.Application中显示本地图片 ? 对VBS效率的再思考--处理二进制数据 WordPress判断用户是否登录 ? 在InternetExplorer.Application中显示本地图片 标题: 在InternetExplorer.Application中显示本地图片作者: Demon链接: http://demon.tw/programming/internetexplor

Eclipse使用及其问题解决方案

Eclipse使用:1.如何把Eclipse关闭提示调出来?可以这样打开这个提示:选择 Windows --Preferences,在左边树上选择“General” --“Startup and Shutdown”,选择“Confirm exit when closing last window ”单击 OK 就可以了 2.Eclipse编辑框右面长度100的位置设置一条线Windows --->Preferences--->General--->Editors--->Text E

sublime 设置

// While you can edit this file, it's best to put your changes in // "User/Preferences.sublime-settings", which overrides the settings in here. // // Settings may also be placed in file type specific options files, for // example, in Packages/Py

Swing-JOptionPane对话框用法-入门

对话框是GUI程序中常见的界面,通常用来反馈提示信息.告警或获取用户输入.比如这种: JOptionPane是Swing中的一个对话框类,它能够提供常见的绝大多数对话框效果,本文对这个类进行介绍.需要说明的有两点:1.JOptionPane所提供的对话框是模态的,也就是说对话框会阻塞原窗口的显示:如果要创建一个非模态对话框,必须使用JDialog类.2.swing另外提供了JFileChooser类(文件选择器)和JColorChooser(颜色选择器),这些对话框比较特殊,以后专门再讲. JO

sublime text2卸载和重新安装(转载)

很多同学使用 sublime text2 的时候,出现一些奇怪的bug,且重启无法修复. 于是,就会想到卸载 sublime text2 再重新安装. 然而,你会发现,重新安装后,这个bug任然存在,且你之前安装过的插件也都还保留着. 这是因为,卸载的时候只卸载了软件部分,而其配置文件却未被删除. 如果想得到一个全新的 sublime text2 软件,你只需要删除配置文件,重新启动 sublime text2 软件的时候,就会重新生成一个全新的干净的配置文件.就如同你第一次安装 sublime