尽管有线程,tkinter 仍然冻结 [英] tkinter freezing despite threading

查看:78
本文介绍了尽管有线程,tkinter 仍然冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个网络扫描器,它会持续扫描易受攻击的 URL.我想为我的程序提供一个 GUI,因此选择使用 tkinter.

I am currently developing a web scanner that continuously scans for vulnerable URLs. I wanted a GUI for my program and thus chose to use tkinter.

目前我面临一个问题,当我通过按一个按钮到其他脚本从我的主程序(GUI)调用时,GUI 将冻结并且只有在程序完成扫描整个词表时才会加载.

Currently I am facing a problem where when I call from my main program (the GUI) by pressing a button to the other scripts, the GUI will freeze and will only load up when the program is done scanning with the entire wordlist.

我了解线程并尝试将其应用到我的程序中.但它似乎不起作用,我不确定为什么.

I know of threading and have tried to apply it to my program. But it doesn't seem to be working and I am unsure why.

我已经浏览了许多 stackoverflow 页面几个小时并实施了它们,但它似乎仍然没有奏效.我试过的一个例子是 python在后台运行任务,同时允许 tkinter 处于活动状态

I've browsed through many stackoverflow pages for hours and implementing them and it still doesn't seem to work out. One example I've tried is python running task in the background while allowing tkinter to be active

我的代码如下:

主界面

from Tkinter import *
from URLFuzzerNonRecursive import nonrecursiveURL
import threading

def thread(value):
   t = threading.Thread(target=nonrecursiveURL(value))
   t.start()
   #t.join()

def main():
   master = Tk()
   master.title("Web Fuzzer")

   master.geometry("700x400")
   urllabel = Label(master, text="Input URL to Fuzz")
   urllabel.place(relx=0.35, rely=0.05)
   urlinput = Entry(master, bd =5)
   urlinput.place(relx=0.35, rely=0.10)
   urlinput.insert(END,"http://")

   nonrecursionURL = Button(master, text="Non-Recursive URL Fuzzer", command=lambda: thread(urlinput.get()),width=20, height=2)
   nonrecursionURL.place(relx=0.35, rely=0.30)

   master.mainloop()

if __name__ == '__main__':
   main()

主 GUI 调用的 Python 脚本:

Python Script called by main GUI:

import requests
import tkinter.messagebox as tkmb
import tkinter as tk
import threading

def nonrecursiveURL(URLtoTake):
   root = tk.Tk()

   ybar = tk.Scrollbar(root)
   T = tk.Text(root)
   ybar.config(command=T.yview)
   T.config(yscrollcommand=ybar.set)
   T.grid(row=0, column=0, columnspan=5)
   ybar.grid(row=0, column=5, sticky="ns")
   try:
    with open('wordlist.txt') as f:
        for line in f:
            website = URLtoTake.strip('\n')+"/" + line
            URL = requests.get(website)
            if URL.status_code == 200:
                foundsites.append(website)
                info_message = "Status code: "+ str(URL.status_code) + " Found :" + website
                T.insert(tk.END,info_message)

            else:
                info_message = "Status code: "+ str(URL.status_code) +" Not Found :" + website
                T.insert(tk.END,info_message)

        for i in foundsites:
            T.insert(tk.END,"\n")
            T.insert(tk.END,"Found : ")
            T.insert(tk.END, i)
   except requests.exceptions.RequestException:
       info_message = "Please enter a valid URL ( inclusive of http:// )"
       tkmb.showinfo("Output", info_message)

推荐答案

问题中贴出的thread函数是调用nonrecursiveURL函数直接,在将其作为线程目标传递之前.

The thread function posted in the question is calling the nonrecursiveURL function directly, before passing it as the thread target.


def thread(value):
   t = threading.Thread(target=nonrecursiveURL(value)) <-- here nonerecursiveURL is being called

要在不同的线程中调用函数,threading.Thread 实例需要一个函数引用,以及要传递给该函数的可选参数列表.

To call a function in a different thread, the threading.Thread instance requires a function reference, and an optional list of arguments to be passed to that function.

t = threading.Thread(target=nonrecursiveURL, args=(value,))

现在nonrecursiveURL在这里没有被调用,而是只传递给Thread,并且会在不同的线程中调用,当t.start() 被调用.

now nonrecursiveURL is not called in here, but only passed to the Thread, and will be called in a different thread, when t.start() is called.

您可以确认这个问题,只需从 Python REPL 调用 thread 函数,将 Tkinter 排除在调试会话之外.

You can confirm this issue, by just calling the thread function from Python REPL, leaving Tkinter out of the debugging session.

这篇关于尽管有线程,tkinter 仍然冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆