pythontkinter

Cannot get an IntVar object from one tkinter script called by a function to the script which calls it


I'm trying to have an IntVar object move from one script to another via a function that calls the second script when the toplevel window created in the second script is destroyed

main script:

from tkinter import *
import project2
root = Tk()

def openbutton():
    global points
    points = project2.OpenWindow(root)
    points = points.get()
    print(points)
OpenButton = Button(root, text = "open window", command = openbutton)
OpenButton.pack()
root.wait_window(window)
root.mainloop()

Part of the second script that returns the IntVar object:

done = False
from tkinter import *
def OpenWindow(root):
    global done
    score = 10 #placeholder for whatever the real value ends up being
    if done == True:
        return IntVar(root, score)
    window = Toplevel(root)
    
    def end(event):
        global done
        if event.widget == window:
            done = True
            OpenWindow(root)
    window.bind('<Destroy>', end)
    window.mainloop()
#score comes from other code within the OpenWindow function so just
#pretend that it has some integer value assigned to it.

I run into three problems, however:

  1. the openbutton function should continue after the toplevel window is destroyed, printing the points value, however, it doesn't and I have to press its asocciated button again to get it continue the function and print the value.

  2. pressing the button again after the toplevel window has been closed will not open up the second window and will instead only print the points value.

  3. the points variable doesn't exist until the toplevel window is closed and the OpenButton button is pressed for a second time; creating another button to print it points will cause an error unless that criteria is fufilled. (I think this is an effect of the openbutton() function not continuing until the toplevel window is closed and the button is pressed for a second time)

    the points variable doesn't have any value assigned to it after the root window is closed and will return None when printed in the terminal (I don't know how important this is, as the entire project should be within tkinter, but I would like to be able print it out in the terminal)

any help would be appreciated, this whole project has driven me insane.


Solution

  • It seems that you are misusing mainloop() slightly.

    A solution seems to be to be to use window.quit() to exit its mainloop() and return control to the previous mainloop().

    See the changes to the project2 file:

    # main
    from tkinter import *
    import project2
    root = Tk()
    
    def openbutton():
        global points
        points = project2.OpenWindow(root)
        points = points.get()
        print(points)
    OpenButton = Button(root, text = "open window", command = openbutton)
    OpenButton.pack()
    root.mainloop()
    
    
    # project2
    score = 42
    from tkinter import *
    
    def OpenWindow(root):
        window = Toplevel(root)
        
        def end(event):
            if event.widget == window:
                window.quit()
    
        window.bind('<Destroy>', end)
        window.mainloop()
        return IntVar(root, score)
    

    Now the new window in project2 can do what it likes, but when it is closed, the nested end() function is called which quits the new mainloop returning control back to the original mainloop in the main file.