0
votes

Using the button I m executing a script on a separate thread, which after completing needs to come back to the screen where I started from. If I mention that snippet at the end of my script which I m running on a separate thread giving me a TCL error since I m trying to call the Tkinter thread from another thread.

I want to know how to achieve this goal.

I have tried these 2 way..

1st:

#imports

#from the screen1 button I m sending arg to this function to run on seperate thread.

def screen2(arg):
    #this window show the status of script progress bar, label status etc#
    window.geometry("1215x770")
    window.configure(bg="#FFFFFF")

    #background canvas#
    #progressbar#
    #label#

    def task():
    
        if arg = foo:
            #I cant use join here or else it will wait for the script to complete without showing screen2#
            #I tried putting the mainloop after starting the thread, but as it means the tkinter is on hault it wont execute the after command.

            t1 = threading.Thread(target=script, args(ag1, ag2,)).start()
            window.mainloop()
            window.resize(false, false)
            response = messagebox.showinfo("Task", "Script Ran!")
            if response:
                     screen1()

        else:
            t1 = threading.Thread(target=script, args(ag3, ag4,)).start()
            window.mainloop()
            window.resize(false, false)
            response = messagebox.showinfo("Task", "Script Ran!")
            if response:
                     screen1()

    task()


def script(arg1, arg2):
     #Doing something here#

I tried using the while loop to keep checking the status of the thread if it's alive then keep looping with the main loop in it if not go back to screen1. which didn't work.

What else option do I have? I need to fix this temporarily since I m only learning it but I have to present it. I m planning to shift it to another GUI whats the best temporary fix for this? I don't want to write the whole code again because it's multiple screens.

mainloop() runs loop which blocks rest of code. If you want to check thread then you could window.after(milliseconds, function) to run code periodically and check if thread is still alive. But you have to assign thread to global variable to access it in another function.furas
running scrreen1() in screen2() can create recursion. Maybe it could be better to exit screen2 to go back to screen1 - eventually screen2 could return response and run if ouside screen2furas
"temporary fix" is the worst. Your code may need many changes. There should be function which runs screen2 and get result from this function and later it decide if it has to run screen1 - some kind of screens managerfuras
@furas thanks for the quick response. I don't have issue using global variables that's fine, I also tried using windows.after before adding window.mainloop() to check if the thread is alive, and issue if the thread is alive it's not showing screen2 but running the script in the background. and I m not actually trying to run screen1 in screen2 I m just sending arg from screen1 to determine which function to run and show progress bar status and once that function is ran I want to go back to the screen1. I m sorry if its sound confusing, I m just stuck on this since 2 days.nTlearn
screen2 has the progress bar, and label which I m updating through the script. Using window.update() and update_idle() but if I end the script with redirecting to screen1 its failing because the thread I m running my script on is still alive.nTlearn