我的代码中有一些奇怪的东西,我无法在网上找到解决方案(至少我试过)。
代码在1个线程上完美运行。
for i in range(1):
当我尝试增加线程数时,
for i in range(3):
代码停止正常运行(验证码GUI停止显示,没有错误,只是停止显示)
我尝试做的是使用tkinter GUI弹出验证码,供用户解决。
这是我的代码中的一个非常简化的版本,仅用于此帖。
import requests,cStringIO, PIL , Queue
from Functions import *
from PIL import ImageTk
import Tkinter as tk
from Tkinter import *
import threading
__author__ = 'user'
Session = {'view_state': '', 'event_validation': ''}
threadLock = threading.Lock()
class PopCaptcha( Frame ):
def __init__( self , img):
def onok(event=None):
self.captchaText = self.entry1.get()
self.captchaThread = 1
self.master.destroy()
self.captchaThread = 0
self.captchaText = ""
tk.Frame.__init__(self)
self.pack()
self.master.title("Enter Captcha")
image1 = PIL.Image.open(img)
tkpi = ImageTk.PhotoImage(image1)
self.picture1 = Label(self, image=tkpi)
self.picture1.image = tkpi
self.picture1.grid(row= 1)
self.entry1 = Entry(self , font=('Helvetica', '14'))
self.entry1.grid(row= 2)
self.button1 = Button( self, text = "SUBMIT", width = 25,
command = onok )
self.button1.grid( row = 3)
self.master.bind('<Return>', onok)
self.master.protocol("WM_DELETE_WINDOW", self.on_closing)
def on_closing(self):
self.captchaThread = 1
self.master.destroy()
class TestClass:
def __init__(self):
self.error = ""
self.status = ""
self.captchaTries = 0
def DoThing(self):
self.status = ""
self.error = ""
self.captchaThread = 0
captchaurl = "https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha.jpg"
hr = requests.get(captchaurl)
captchafile = cStringIO.StringIO(hr.content)
while self.status is not "OK" or self.error == "WrongCaptcha":
self.captchaTries += 1
if (self.captchaTries > 3):
hr = requests.get(captchaurl)
captchafile = cStringIO.StringIO(hr.content)
self.captchaTries = 0
print "Preparing Captcha..."
threadLock.acquire()
captcha = PopCaptcha(captchafile)
print "Showing Captcha..."
captcha.mainloop()
print "Captcha Showen"
threadLock.release()
while captcha.captchaThread == 0:
pass
Session['Captcha'] = captcha.captchaText
PostData = {
'Captcha' : Session['Captcha']
}
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0',
'X-Requested-With': 'XMLHttpRequest',
'X-MicrosoftAjax' : 'Delta=true',
'Pragma' : 'no-cache',
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Accept-Language' : 'en-US,en;q=0.5'
}
print "Attempting to post ..."
r = requests.post("http://google.com", data=PostData, headers=headers)
if 1:
print "FAILED"
self.error = "WrongCaptcha"
else:
print "WORKED"
self.error = "NoError"
self.status = "OK"
print "Welcome to MD5 Cracker!"
threads = []
for i in range(3):
hk = TestClass()
t = threading.Thread(target=hk.DoThing)
threads.append(t)
for x in threads:
x.start()
print "Thread started"
我的代码有什么问题我不知道吗? 这个问题怎么解决?
由于
答案 0 :(得分:2)
Tkinter不是线程安全的。您正在尝试在多个线程中创建小部件,这很少有效。
可能在每个线程中创建单独的根窗口,但我从未尝试过这样的事情。您可以在没有线程的情况下执行此操作,但很难理解为什么要为每个线程创建小部件。
通常在多线程tkinter应用程序中,您有一个包含小部件的线程,然后其他线程执行数据处理并将更改放在GUI线程读取的队列中以更新其显示。