2012-06-05 28 views
2

QObject:無法爲處於不同線程中的父項創建子項。 (Parent是另外,QTextDocument(0x9919018),父母的線程的QThread(0x97331e0),當前線程被flooderthread(0x97b4c10) 錯誤表示很抱歉,因爲是新來這裏的PyQt 是代碼:pyqt QObject:無法爲處於不同線程中的父項創建子項

我知道代碼是完呢,但它應該工作,我想這個問題是myfun.log功能...

#! /usr/bin/python 
# -*- coding: utf-8 -*- 
import urllib, urllib2, itertools, threading, cookielib, Cookie, sys, time, hashlib, os 
from PyQt4 import QtCore, QtGui 
try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    _fromUtf8 = lambda s: s 
gui=QtGui.QApplication.processEvents 
texttoset="" 
class fun(): 
    global texttoset 
    def checkpassword(self): 
     if ui.passwordcheck.isChecked()==True: 
      return 1 
     else : 
      return 0 
    def log(self, text): 
     if text != False: 
      firsttext=str(ui.console.toPlainText()) 
      secondtext=firsttext+text+"\n" 
      ui.console.setText(secondtext) 
      log=open("log.log", "a") 
      log.write(text+"\n") 
      log.close() 
     else : 
      firsttext=str(ui.console.toPlainText()) 
      secondtext=firsttext+texttoset+"\n" 
      ui.console.setText(secondtext) 
      log=open("log.log", "a") 
      log.write(texttoset+"\n") 
      log.close() 
    def disable(self): 
     MainWindow.setEnabled(False) 
     pass 
    def enable(self): 
     MainWindow.setEnabled(True) 
     pass 
    def checkmethod(self): 
     if ui.get.isChecked()==True: 
      return 1 
     elif ui.post.isChecked()==True: 
      return 2 
     else : 
      return 0 
    def main(self): 
     connecter() 
     gui() 
     f1.start() 
     gui() 
     time.sleep(3) 
     gui() 
     f2.start() 
     gui() 
     time.sleep(3) 
     gui() 
     f3.start() 
     gui() 
     time.sleep(3) 
     gui() 
     f4.start() 
     gui() 
     time.sleep(3) 
     gui() 
     f5.start() 
     gui() 
     self.sleep(3) 
     gui() 
     f6.start() 
     gui() 
    def killer(self): 
     f1.terminate() 
     f2.terminate() 
     f3.terminate() 
     f4.terminate() 
     f5.terminate() 
     f6.terminate() 
    def close(self): 
     self.killer() 
     os.abort() 
     sys.exit() 

myfun=fun() 
def connecter(): 
    QtCore.QObject.connect(f1, QtCore.SIGNAL("log(bool)"), myfun.log) 
    QtCore.QObject.connect(f1, QtCore.SIGNAL("enable()"), myfun.enable) 
    QtCore.QObject.connect(f1, QtCore.SIGNAL("disable()"), myfun.disable) 

    QtCore.QObject.connect(f2, QtCore.SIGNAL("log(bool)"), myfun.log) 
    QtCore.QObject.connect(f2, QtCore.SIGNAL("enable()"), myfun.enable) 
    QtCore.QObject.connect(f2, QtCore.SIGNAL("disable()"), myfun.disable) 


    QtCore.QObject.connect(f3, QtCore.SIGNAL("log(bool)"), myfun.log) 
    QtCore.QObject.connect(f3, QtCore.SIGNAL("enable()"), myfun.enable) 
    QtCore.QObject.connect(f3, QtCore.SIGNAL("disable()"), myfun.disable) 


    QtCore.QObject.connect(f4, QtCore.SIGNAL("log(bool)"), myfun.log) 
    QtCore.QObject.connect(f4, QtCore.SIGNAL("enable()"), myfun.enable) 
    QtCore.QObject.connect(f4, QtCore.SIGNAL("disable()"), myfun.disable) 


    QtCore.QObject.connect(f5, QtCore.SIGNAL("log(bool)"), myfun.log) 
    QtCore.QObject.connect(f5, QtCore.SIGNAL("enable()"), myfun.enable) 
    QtCore.QObject.connect(f5, QtCore.SIGNAL("disable()"), myfun.disable) 


    QtCore.QObject.connect(f6, QtCore.SIGNAL("log(bool)"), myfun.log) 
    QtCore.QObject.connect(f6, QtCore.SIGNAL("enable()"), myfun.enable) 
    QtCore.QObject.connect(f6, QtCore.SIGNAL("disable()"), myfun.disable) 
x=0 
num=0 
class flooderthread(QtCore.QThread): 
    global texttoset 
    def __init__(self, x, num): 
     QtCore.QThread.__init__(self) 
     self.x=x 
     self.num=num 
    def log(self, text): 
     texttolog=str(text) 
     time.sleep(1) 
     self.emit(QtCore.SIGNAL("log(bool)"), False) 
     time.sleep(2) 
    def enable(self): 
     time.sleep(1) 
     self.emit(QtCore.SIGNAL("enable()")) 
    def disable(self): 
     time.sleep(1) 
     self.emit(QtCore.SIGNAL("disable()")) 
    def run(self): 
     connecter() 
     self.log("\n\n--------------------------------------------------new session-------------------------------------\n\n") 
     itered=False 
     gui() 
     self.disable() 
     gui() 
     self.log("setting params...") 
     param={ui.dataname1.text():ui.datavalue1.text(),ui.dataname3.text():ui.datavalue3.text(),ui.dataname3.text():ui.datavalue3.text(), } 
     self.log("checking password...") 
     if myfun.checkpassword()==1: 
      itered=True 
      self.log("password is true") 
     else : 
      self.log("password is null ") 
      self.log("itered operation") 
     self.log("setting url") 
     url=str(ui.url.text()) 
     if url[:4]!="http" and url[:3]!="ftp": 
      self.log("url error exiting the whole function") 
      self.log("please set a valide protocole!!") 
      gui() 
      self.enable() 
      gui() 
      return 1 
      pass 
     else : 
      self.log("valid url") 
      gui() 
      self.log("url is "+url) 
     self.log("setting proxy") 
     proxy="http://"+ui.proxyuser.text()+":"+ui.proxypass.text()+"@"+ui.proxyhost.text()+":"+ui.proxyport.text() 
     self.log("proxy is "+proxy) 
     gui() 
     self.log("preparing params...") 
     urlparam=urllib.urlencode(param) 
     gui() 
     self.log("params are "+urlparam) 
     self.log("setting up headers...") 
     header={'User-Agent':str(ui.useragent.toPlainText())} 
     self.log("headers are "+ str(header)) 
     self.log("setting up proxy handler..") 
     proxyhandler=urllib2.ProxyHandler({"http":str(proxy)}) 
     self.log("checking method") 
     if myfun.checkmethod()==1: 
      self.log("method is get..") 
      self.log("setting request..") 
      finalurl=url+urlparam 
      gui() 
      self.log("final url is"+finalurl) 
      req=urllib2.Request(finalurl, None, headers) 
     elif myfun.checkmethod()==2: 
      self.log("method is post...") 
      self.log("setting request..") 
      finalurl=url 
      gui() 
      self.log("final url is "+finalurl) 
      req=urllib2.Request(finalurl, urlparam, header) 
     else : 
      self.log("error has been accourded") 
      self.log("please select a method!!") 
      gui() 
      self.log("exiting the whole functions") 
      gui() 
      self.enable() 
      return 1 
      pass 
     self.log("intilizing cookies..") 
     c1=Cookie.SimpleCookie() 
     c1[str(ui.cookiename1.text())]=str(ui.cookievalue1.text()) 
     c1[str(ui.cookiename1.text())]['path']='/' 
     c1[str(ui.cookiename2.text())]=str(ui.cookievalue2.text()) 
     c1[str(ui.cookiename2.text())]['path']='/' 
     c1[str(ui.cookiename3.text())]=str(ui.cookievalue3.text()) 
     c1[str(ui.cookiename3.text())]['domain']=url 
     c1[str(ui.cookiename3.text())]['path']='/' 
     c1[str(ui.cookiename4.text())]=str(ui.cookievalue4.text()) 
     c1[str(ui.cookiename4.text())]['domain']=url 
     c1[str(ui.cookiename4.text())]['path']='/' 
     self.log("cookies are.. :"+str(c1)) 
     cj=cookielib.CookieJar() 
     cj.set_cookie(c1) 
     opener = urllib2.build_opener(proxyhandler, urllib2.HTTPCookieProcessor(cj)) 
     self.log("insatlling opener") 
     urllib2.install_opener(opener) 
     self.log("setting the two operations....") 
     if itered==Fasle: 
      self.log("starting the flooding loop") 
      gui() 
      while true: 
       try: 
        gui() 
        opener.open(req) 
       except e: 
        self.log("error connecting : "+e.reason) 
        self.log("will continue....") 
        continue 
       gui() 

     elif itered==True: 
      pass 
f1=flooderthread(1, 1) 
f2=flooderthread(2, 2) 
f3=flooderthread(3, 3) 
f4=flooderthread(4, 4) 
f5=flooderthread(5, 5) 
f6=flooderthread(6, 6) 
class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName(_fromUtf8("MainWindow")) 
     MainWindow.setMinimumSize(QtCore.QSize(838, 500)) 
     MainWindow.setMaximumSize(QtCore.QSize(838, 500)) 
     MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "memo flooder", None, QtGui.QApplication.UnicodeUTF8)) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.console=QtGui.QTextEdit(self.centralwidget) 
     self.console.setGeometry(10, 350, 800,130) 
     self.console.setReadOnly(True) 
     self.console.setObjectName("console") 
     self.groupBox = QtGui.QGroupBox(self.centralwidget) 
     self.groupBox.setGeometry(QtCore.QRect(30, 50, 71, 80)) 
     self.groupBox.setTitle(QtGui.QApplication.translate("MainWindow", "method:", None, QtGui.QApplication.UnicodeUTF8)) 
     self.groupBox.setObjectName(_fromUtf8("groupBox")) 
     self.post = QtGui.QRadioButton(self.groupBox) 
     self.post.setGeometry(QtCore.QRect(10, 20, 61, 22)) 
     self.post.setText(QtGui.QApplication.translate("MainWindow", "post", None, QtGui.QApplication.UnicodeUTF8)) 
     self.post.setChecked(True) 
     self.post.setObjectName(_fromUtf8("post")) 
     self.get = QtGui.QRadioButton(self.groupBox) 
     self.get.setGeometry(QtCore.QRect(10, 50, 51, 22)) 
     self.get.setText(QtGui.QApplication.translate("MainWindow", "get", None, QtGui.QApplication.UnicodeUTF8)) 
     self.get.setObjectName(_fromUtf8("get")) 
     self.url = QtGui.QLineEdit(self.centralwidget) 
     self.url.setGeometry(QtCore.QRect(70, 20, 671, 27)) 
     self.url.setInputMethodHints(QtCore.Qt.ImhUrlCharactersOnly) 
     self.url.setObjectName(_fromUtf8("url")) 
     self.groupBox_2 = QtGui.QGroupBox(self.centralwidget) 
     self.groupBox_2.setGeometry(QtCore.QRect(110, 50, 371, 111)) 
     self.groupBox_2.setTitle(QtGui.QApplication.translate("MainWindow", "data:", None, QtGui.QApplication.UnicodeUTF8)) 
     self.groupBox_2.setObjectName(_fromUtf8("groupBox_2")) 
     self.dataname1 = QtGui.QLineEdit(self.groupBox_2) 
     self.dataname1.setGeometry(QtCore.QRect(20, 30, 101, 27)) 
     self.dataname1.setObjectName(_fromUtf8("dataname1")) 
     self.label = QtGui.QLabel(self.groupBox_2) 
     self.label.setGeometry(QtCore.QRect(40, 10, 67, 17)) 
     self.label.setText(QtGui.QApplication.translate("MainWindow", "name:", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label.setObjectName(_fromUtf8("label")) 
     self.dataname2 = QtGui.QLineEdit(self.groupBox_2) 
     self.dataname2.setGeometry(QtCore.QRect(130, 30, 113, 27)) 
     self.dataname2.setObjectName(_fromUtf8("dataname2")) 
     self.dataname3 = QtGui.QLineEdit(self.groupBox_2) 
     self.dataname3.setGeometry(QtCore.QRect(250, 30, 113, 27)) 
     self.dataname3.setObjectName(_fromUtf8("dataname3")) 
     self.label_2 = QtGui.QLabel(self.groupBox_2) 
     self.label_2.setGeometry(QtCore.QRect(40, 60, 67, 17)) 
     self.label_2.setText(QtGui.QApplication.translate("MainWindow", "value:", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label_2.setObjectName(_fromUtf8("label_2")) 
     self.datavalue1 = QtGui.QLineEdit(self.groupBox_2) 
     self.datavalue1.setGeometry(QtCore.QRect(20, 80, 101, 27)) 
     self.datavalue1.setObjectName(_fromUtf8("datavalue1")) 
     self.datavalue2 = QtGui.QLineEdit(self.groupBox_2) 
     self.datavalue2.setGeometry(QtCore.QRect(130, 80, 113, 27)) 
     self.datavalue2.setObjectName(_fromUtf8("datavalue2")) 
     self.datavalue3 = QtGui.QLineEdit(self.groupBox_2) 
     self.datavalue3.setGeometry(QtCore.QRect(250, 80, 113, 27)) 
     self.datavalue3.setObjectName(_fromUtf8("datavalue3")) 
     self.groupBox_4 = QtGui.QGroupBox(self.centralwidget) 
     self.groupBox_4.setGeometry(QtCore.QRect(670, 50, 151, 111)) 
     self.groupBox_4.setTitle(QtGui.QApplication.translate("MainWindow", "password:", None, QtGui.QApplication.UnicodeUTF8)) 
     self.groupBox_4.setObjectName(_fromUtf8("groupBox_4")) 
     self.passname = QtGui.QLineEdit(self.groupBox_4) 
     self.passname.setGeometry(QtCore.QRect(10, 30, 113, 27)) 
     self.passname.setObjectName(_fromUtf8("passname")) 
     self.passvalue = QtGui.QLineEdit(self.groupBox_4) 
     self.passvalue.setGeometry(QtCore.QRect(10, 80, 113, 27)) 
     self.passvalue.setObjectName(_fromUtf8("passvalue")) 
     self.passwordcheck = QtGui.QCheckBox(self.centralwidget) 
     self.passwordcheck.setGeometry(QtCore.QRect(670, 180, 97, 22)) 
     self.passwordcheck.setText(QtGui.QApplication.translate("MainWindow", "password", None, QtGui.QApplication.UnicodeUTF8)) 
     self.passwordcheck.setChecked(True) 
     self.passwordcheck.setObjectName(_fromUtf8("passwordcheck")) 
     self.groupBox_5 = QtGui.QGroupBox(self.centralwidget) 
     self.groupBox_5.setGeometry(QtCore.QRect(29, 169, 441, 81)) 
     self.groupBox_5.setTitle(QtGui.QApplication.translate("MainWindow", "proxy:", None, QtGui.QApplication.UnicodeUTF8)) 
     self.groupBox_5.setObjectName(_fromUtf8("groupBox_5")) 
     self.proxyhost = QtGui.QLineEdit(self.groupBox_5) 
     self.proxyhost.setGeometry(QtCore.QRect(20, 30, 113, 27)) 
     self.proxyhost.setObjectName(_fromUtf8("proxyhost")) 
     self.proxyport = QtGui.QLineEdit(self.groupBox_5) 
     self.proxyport.setGeometry(QtCore.QRect(140, 30, 51, 27)) 
     self.proxyport.setInputMethodHints(QtCore.Qt.ImhDigitsOnly|QtCore.Qt.ImhPreferNumbers) 
     self.proxyport.setObjectName(_fromUtf8("proxyport")) 
     self.proxyuser = QtGui.QLineEdit(self.groupBox_5) 
     self.proxyuser.setGeometry(QtCore.QRect(200, 30, 113, 27)) 
     self.proxyuser.setObjectName(_fromUtf8("proxyuser")) 
     self.proxypass = QtGui.QLineEdit(self.groupBox_5) 
     self.proxypass.setGeometry(QtCore.QRect(320, 30, 113, 27)) 
     self.proxypass.setObjectName(_fromUtf8("proxypass")) 
     self.label_4 = QtGui.QLabel(self.groupBox_5) 
     self.label_4.setGeometry(QtCore.QRect(100, 10, 67, 17)) 
     self.label_4.setText(QtGui.QApplication.translate("MainWindow", "host", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label_4.setObjectName(_fromUtf8("label_4")) 
     self.label_5 = QtGui.QLabel(self.groupBox_5) 
     self.label_5.setGeometry(QtCore.QRect(150, 10, 67, 17)) 
     self.label_5.setText(QtGui.QApplication.translate("MainWindow", "port", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label_5.setObjectName(_fromUtf8("label_5")) 
     self.label_6 = QtGui.QLabel(self.groupBox_5) 
     self.label_6.setGeometry(QtCore.QRect(200, 10, 67, 17)) 
     self.label_6.setText(QtGui.QApplication.translate("MainWindow", "username", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label_6.setObjectName(_fromUtf8("label_6")) 
     self.label_7 = QtGui.QLabel(self.groupBox_5) 
     self.label_7.setGeometry(QtCore.QRect(320, 10, 67, 17)) 
     self.label_7.setText(QtGui.QApplication.translate("MainWindow", "password", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label_7.setObjectName(_fromUtf8("label_7")) 
     self.groupBox_6 = QtGui.QGroupBox(self.centralwidget) 
     self.groupBox_6.setGeometry(QtCore.QRect(30, 260, 531, 91)) 
     self.groupBox_6.setTitle(QtGui.QApplication.translate("MainWindow", "cookies:", None, QtGui.QApplication.UnicodeUTF8)) 
     self.groupBox_6.setObjectName(_fromUtf8("groupBox_6")) 
     self.cookiename1 = QtGui.QLineEdit(self.groupBox_6) 
     self.cookiename1.setGeometry(QtCore.QRect(10, 20, 113, 27)) 
     self.cookiename1.setObjectName(_fromUtf8("cookiename1")) 
     self.cookiename2 = QtGui.QLineEdit(self.groupBox_6) 
     self.cookiename2.setGeometry(QtCore.QRect(140, 20, 113, 27)) 
     self.cookiename2.setObjectName(_fromUtf8("cookename2")) 
     self.cookiename3 = QtGui.QLineEdit(self.groupBox_6) 
     self.cookiename3.setGeometry(QtCore.QRect(270, 20, 113, 27)) 
     self.cookiename3.setObjectName(_fromUtf8("cookiename3")) 
     self.cookiename4 = QtGui.QLineEdit(self.groupBox_6) 
     self.cookiename4.setGeometry(QtCore.QRect(390, 20, 113, 27)) 
     self.cookiename4.setObjectName(_fromUtf8("cookiename4")) 
     self.cookievalue1 = QtGui.QLineEdit(self.groupBox_6) 
     self.cookievalue1.setGeometry(QtCore.QRect(10, 50, 113, 27)) 
     self.cookievalue1.setObjectName(_fromUtf8("cookievalue1")) 
     self.cookievalue2 = QtGui.QLineEdit(self.groupBox_6) 
     self.cookievalue2.setGeometry(QtCore.QRect(140, 50, 113, 27)) 
     self.cookievalue2.setObjectName(_fromUtf8("cookievalue2")) 
     self.cookievalue3 = QtGui.QLineEdit(self.groupBox_6) 
     self.cookievalue3.setGeometry(QtCore.QRect(270, 50, 113, 27)) 
     self.cookievalue3.setObjectName(_fromUtf8("cookievalue3")) 
     self.cookievalue4 = QtGui.QLineEdit(self.groupBox_6) 
     self.cookievalue4.setGeometry(QtCore.QRect(390, 50, 113, 27)) 
     self.cookievalue4.setObjectName(_fromUtf8("cookievalue4")) 
     self.groupBox_7 = QtGui.QGroupBox(self.centralwidget) 
     self.groupBox_7.setGeometry(QtCore.QRect(570, 260, 251, 80)) 
     self.groupBox_7.setTitle(QtGui.QApplication.translate("MainWindow", "useragents:", None, QtGui.QApplication.UnicodeUTF8)) 
     self.groupBox_7.setObjectName(_fromUtf8("groupBox_7")) 
     self.useragent = QtGui.QTextEdit(self.groupBox_7) 
     self.useragent.setGeometry(QtCore.QRect(10, 20, 211, 51)) 
     self.useragent.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) 
     self.useragent.setObjectName(_fromUtf8("useragent")) 
     self.start = QtGui.QPushButton(self.centralwidget) 
     self.start.setGeometry(QtCore.QRect(750, 20, 71, 27)) 
     self.start.setText(QtGui.QApplication.translate("MainWindow", "start", None, QtGui.QApplication.UnicodeUTF8)) 
     self.start.setObjectName(_fromUtf8("start")) 
     self.label_3 = QtGui.QLabel(self.centralwidget) 
     self.label_3.setGeometry(QtCore.QRect(30, 20, 67, 17)) 
     self.label_3.setText(QtGui.QApplication.translate("MainWindow", "url :", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label_3.setObjectName(_fromUtf8("label_3")) 
     MainWindow.setCentralWidget(self.centralwidget) 
     QtCore.QObject.connect(self.start, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), myfun.main) 
     QtCore.QObject.connect(self.passwordcheck, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.groupBox_4.setEnabled) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 


def __del__(): 
    myfun.killer() 
    os.abort() 
    sys.exit() 

app = QtGui.QApplication(sys.argv) 
MainWindow = QtGui.QMainWindow() 
ui = Ui_MainWindow() 
ui.setupUi(MainWindow) 
myfun.log("\n\n--------------------------------------------------new session-------------------------------------\n\n") 
MainWindow.show() 
sys.exit(app.exec_()) 
+2

您能否將代碼縮小到實際發生錯誤的位置,而不是發佈400行代碼塊? – Andy

+1

此代碼正在讓我1.5歲的女兒現在哭: -/ – jdi

+0

:P很抱歉,但我不知道錯誤行在哪裏。 – memomk

回答

2

我幾乎不知道什麼在你的代碼怎麼回事,在其意圖方面,但這裏是有問題的行代碼導致您的崩潰:

class flooderthread(QtCore.QThread): 
    ... 
    def run(self): 
     connecter() # this is the culprit 

在您的fun.main()中,您可以撥打connecter()來設置您的所有信號/插槽連接。此調用正在您的主線程中發生。但是,然後你有每個線程也調用該方法,這是試圖跨線程重複連接到對象。實際的崩潰可能比這個函數更深,因爲它是信號發射的結果,但是我發現跳到代碼中來實現更深入的代碼太困難了。很可能你有線程在不應該的時候直接修改GUI元素。

一旦你有一個固定的,你有一個錯字等,這將使它崩潰:

def main(self): 
    connecter() 
    ... 
    time.sleep(3) 
    gui() 
    f5.start() 
    gui() 
    self.sleep(3) # should be time.sleep(3) 
    gui() 
    ... 
從這個

除此之外,我只是想指出的是,這個腳本是非常難追。你正在使用全局變量,所以很難知道在哪裏定義了某些變量和函數調用。你必須滾動一堆,追逐它們。您可能想要在繼續​​滾雪球之前停止並重新組織它。
我會高度建議拿起這樣的書:http://www.qtrac.eu/pyqtbook.html
你應該關注的是組織適當的類。您目前的課程結構需要大量的複習,所以您甚至可能想要選擇一本好的Python書籍來首先學習如何編寫課程。我意識到你只是在學習,這就是爲什麼我希望這個建議能幫助你引導你正確的方向。

+0

thanx很多,因爲我說我還沒有完成代碼......並感謝這本書,我正在尋找這樣的東西:) – memomk

相關問題