0

Im writing an app using tkinter, and the app when the button is pressed connects to sql DB and runs sql queries. I am running into an issue where the GUI says "not responding" while the sql querys method is running, and when its done it goes back to normal. I decided to put the sql query in its own thread and the mainloop of tkinter in its own thread, but its not working at all...?

def createGUI():
    window = Tk()
    window.title("Recon Report Generator")

    lbl=Label(window, text="Enter Recon File Below", fg='blue', font=("Helvetica", 16))
    lbl.place(x=90,y=50)
    textBox=Entry(window, text="This is Entry Widget", bd=5)
    textBox.place(x=135, y=100)
    button=Button(window, height=1, width=15, text="Find Missing", 
                    command=lambda: retrieve_input(textBox,lbl))
    button.place(x=140, y=150)
    window.geometry("400x400")
    window.resizable(False, False)
    _thread.start_new_thread(window.mainloop(), (0,))

This is my GUI I put the mainloop in thread 0

and here

def retrieve_input(textBox,lbl):
    detFile = "'"+(textBox.get())+"'"
    lbl.place(x=140,y=50)
    lbl.config(text="Enter Recon File Below")
    _thread.start_new_thread(runDatabaseQuerys(detFile), (1,))

    lbl.config(text="Finished...")
    print("Finished Please check file")

i put the sql query running method in thread 1 they are indepnedant threads, but i am still getting "not responding"???

fsdff
  • 611
  • 4
  • 10
  • 4
    You aren't actually running anything in a thread. You are actually calling `window.mainloop()` and `runDatabaseQuerys(detFile)` *immediately*, in the main thread, and then trying to use their return values as the function to run in a thread. – jasonharper Jan 22 '20 at 17:46
  • Read [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) and [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Jan 22 '20 at 18:19

1 Answers1

0

I was stuck with this issue for a long time until I found an answer on stackoverflow about using multi-threading.

Tkinter window says (not responding) but code is running

You to create a thread for sql queries function, like this

from threading import Thread 

Thread(target = retrieve_input, args =(textbox, lbl, )).start()
ASAD HAMEED
  • 2,296
  • 1
  • 20
  • 36