为什么我的python代码只有在我使用多线程Tkin时才能工作

2024-04-24 22:34:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码中有一些奇怪的地方,我在网上找不到任何解决方案(至少我试过)。你知道吗

代码在一个线程上完美运行。你知道吗

for i in range(1):

当我尝试增加线程数时

for i in range(3):

代码停止正确运行(captcha GUI停止显示,没有错误,只是停止显示)

我想做的是,弹出一个验证码使用tkintergui的用户来解决。你知道吗

这是一个非常简化的版本我的代码,只是为了这篇文章。你知道吗

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"

我的代码有什么我不知道的错误吗? 如何解决这个问题?你知道吗

谢谢


Tags: 代码inimportselfmasterfordefstatus
1条回答
网友
1楼 · 发布于 2024-04-24 22:34:21

Tkinter不是线程安全的。您正在尝试在多个线程中创建小部件,这很少起作用。你知道吗

也许可以在每个线程中创建单独的根窗口,但我从未尝试过这种方法。您可能不需要线程就可以做到这一点,但是很难理解为什么要为每个线程创建小部件。你知道吗

通常在多线程tkinter应用程序中,只有一个线程包含小部件,然后其他线程进行数据处理,并将更改放在GUI线程读取的队列上以更新其显示。你知道吗

相关问题 更多 >