3

我是python的初学者,我写了一个小程序,当我退出程序时,有时(超过50%的概率)它会显示错误。这是我退出程序后才出现的。请你帮我看看是我的代码有问题。

真的感谢。

ps:我在win7 sp1 x64上使用python3.3和pyqt4

错误:

 问题签名:
 问题事件名称:    BEX
 应用程序名: python.exe
 应用程序版本:    0.0.0.0
 应用程序时间戳:   5150c40a
 故障模块名称:    StackHash_0a9e
 故障模块版本:    0.0.0.0
 故障模块时间戳:   00000000
 异常偏移:  00388100
 异常代码:  c0000005
 异常数据:  00000008
 OS 版本: 6.1.7601.2.1.0.256.1
 区域设置 ID:   2052
 其他信息 1:    0a9e
 其他信息 2:    0a9e372d3b4ad19135b953a78882e789
 其他信息 3:    0a9e
 其他信息 4:    0a9e372d3b4ad19135b953a78882e789

控制台返回这个:

进程以退出代码 -1073741819 结束

主要代码:

import urllib
import http.cookiejar
import time
import sys
from bs4 import BeautifulSoup
from PyQt4 import QtGui
from PyQt4 import QtCore


class BaiduMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(BaiduMainWindow, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.centralwidget = BaiduWidget(self)
        self.setCentralWidget(self.centralwidget)
        self.resize(600, 400)
        self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center())
        self.statusBar().showMessage('Ready')


class BaiduWidget(QtGui.QWidget):
    def __init__(self, parent):
        super(BaiduWidget, self).__init__(parent)
        self.initUI()

    def initUI(self):
        lb = QtGui.QLabel('Search Word')
        self.keyWordEdit = QtGui.QLineEdit()
        self.OKButton = QtGui.QPushButton('Search')
        self.containFilter = QtGui.QLineEdit()
        self.exceptFilter = QtGui.QLineEdit()
        self.result = QtGui.QTextBrowser()

        spacerItem = QtGui.QSpacerItem(30, 50)

        grid = QtGui.QGridLayout()
        grid.setSpacing(10)
        grid.addItem(spacerItem, 0, 0)
        grid.addWidget(lb, 1, 0)
        grid.addWidget(self.keyWordEdit, 2, 0)
        grid.addWidget(self.OKButton, 5, 0)
        grid.addItem(spacerItem, 7, 0)
        grid.addWidget(self.result, 0, 1, 8, 1)
        grid.setColumnStretch(1, 4)
        self.setLayout(grid)



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    form = BaiduMainWindow()
    form.show()
    sys.exit(app.exec_())
4

1 回答 1

3

问题是:spacerItem已经加了grid两次。当你退出程序时,这个对象可能会被 Qt 释放两次。


    def initUI(self):
        lb = QtGui.QLabel('Search Word')
        self.keyWordEdit = QtGui.QLineEdit()
        self.OKButton = QtGui.QPushButton('Search')
        self.containFilter = QtGui.QLineEdit()
        self.exceptFilter = QtGui.QLineEdit()
        self.result = QtGui.QTextBrowser()

        spacerItem = QtGui.QSpacerItem(30, 50)

        grid = QtGui.QGridLayout()
        grid.setSpacing(10)
        grid.addItem(spacerItem, 0, 0)
        grid.addWidget(lb, 1, 0)
        grid.addWidget(self.keyWordEdit, 2, 0)
        grid.addWidget(self.OKButton, 5, 0)
        grid.addItem(spacerItem, 7, 0)
        grid.addWidget(self.result, 0, 1, 8, 1)
        grid.setColumnStretch(1, 4)
        self.setLayout(grid)

您应该使用两个QSpacerItems。


    def initUI(self):
        lb = QtGui.QLabel('Search Word')
        self.keyWordEdit = QtGui.QLineEdit()
        self.OKButton = QtGui.QPushButton('Search')
        self.containFilter = QtGui.QLineEdit()
        self.exceptFilter = QtGui.QLineEdit()
        self.result = QtGui.QTextBrowser()

        spacerItem = QtGui.QSpacerItem(30, 50)
        spacerItem2 = QtGui.QSpacerItem(30, 50)

        grid = QtGui.QGridLayout()
        grid.setSpacing(10)
        grid.addItem(spacerItem, 0, 0)
        grid.addWidget(lb, 1, 0)
        grid.addWidget(self.keyWordEdit, 2, 0)
        grid.addWidget(self.OKButton, 5, 0)
        grid.addItem(spacerItem2, 7, 0)
        grid.addWidget(self.result, 0, 1, 8, 1)
        grid.setColumnStretch(1, 4)
        self.setLayout(grid)

于 2013-04-06T10:09:29.637 回答