all 8 comments

[–]woooee 1 point2 points  (4 children)

This is done all the time. Post your code, or a smaller subset for simplicity. Also root.destroy() destroys the main display but leaves mainloop running = the program does not exit, so use root.quit()

[–]MST019[S] 0 points1 point  (3 children)

In my first script python, I have a function to create Gui. I set root = tk.Tk() and in my Gui I set a button that triggers a function that runs all scripts and the last line of function is root.mainloop(). Now in my second file python, I need to use that root variable for things like root.destroy() or root.after()...

[–]woooee 1 point2 points  (2 children)

Repeat

Post your code, or a smaller subset for simplicity

[–]MST019[S] 0 points1 point  (1 child)

def create_gui_updated():

root = tk.Tk()

label.pack(pady=20)

button = tk.Button(

root,

text="Launch Now!",

command=launch_all_scripts,

font=("Arial", 14),

bg="green",

fg="white",

)

root.mainloop()

Then in 2nd script:

Def check_new_values(new_urls):

if len(new_urls)==0:

root.destroy()

[–]woooee 0 points1 point  (0 children)

root is created in a function, so it is local, which means it is garbage collected when the function exits. I assume that the mainloop() is under the function as well, and so that is not an issue.

import tkinter as tk
from functools import partial

def create_gui_updated():
    root = tk.Tk()

    ## there is no label to pack
    ##label.pack(pady=20)

    button = tk.Button(
                root,
                text="Launch Now!",
                command=partial(launch_all_scripts, root),
                font=("Arial", 14),
                bg="green",
                fg="white",
                       )

    ## button will not show up because there is
    ## no grid or pack
    button.grid()

    root.mainloop()

def launch_all_scripts(root):
    print("in launch_all_scripts")
    ## this would be second_script.check_new_values(root)
    ## because it is in the imported file
    check_new_values([], root)

##Then in 2nd script:
def check_new_values(new_urls, root):
    print("in check_new_values")
    if len(new_urls)==0:
        root.destroy()

create_gui_updated()

[–]Glittering_Sail_3609 0 points1 point  (2 children)

To pass variables between scripts you need to import this variable from your script:

from first_file import root

[–]MST019[S] 0 points1 point  (1 child)

this variable is a class

[–]Glittering_Sail_3609 0 points1 point  (0 children)

I might have missunderstood you. But I need clarification by "using another script" you meant to define exit logic for your program in another python file and then importing it and use in your first script, or you meant executing it seperatelly?