我是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_())
答案 0 :(得分: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)
您应该使用两个QSpacerItem
。
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)