PyQt4 窗口没有正确关闭信号和插槽,只有“x"按钮 [英] PyQt4 window not properly closing with signals and slots, only "x" button

查看:57
本文介绍了PyQt4 窗口没有正确关闭信号和插槽,只有“x"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的,当我创建一个按钮或用于结束程序的菜单选项时,窗口不会关闭.

As the title suggests, when I create a push button, or a menu option for ending the program, the window doesn't close.

所以我试图找出结束程序并同时关闭窗口的方法.我一直在使用教程:

so I'm trying to figure out to end the program and close the window simultaneously. I've been using the tutorial:

http://zetcode.com/tutorials/pyqt4/

否则很棒.那么我如何将按钮与结尾 & 连接起来?关闭小部件?

which is great otherwise. So how do i connect the push button with ending & closing a widget?

这是我一直在使用的一些示例代码(从教程中复制).我似乎无法完全复制结尾,但无论如何我认为这不是问题:

Here is some sample code (copied from the tutorial) that I have been using. I cant seem to get the ending to copy exactly, but I don't think that's the issue anyways:

#!/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()

谢谢!

推荐答案

代码有效.我有同样的问题,但在 IDLE 之外运行它可以工作.无需更改代码即可调用 QtGui 模块上的退出方法.

The code works. I have the same issue, but running it outside IDLE it works. No need to change the code to call the quit method on the QtGui module.

这篇关于PyQt4 窗口没有正确关闭信号和插槽,只有“x"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆