0

I have a script called GUI running two tkinter windows and I've been able to kill my program from within the script by creating a function for it

def gui_kill():
    root.destroy()
    win.destroy()

However, under certain conditions, I am trying to kill them from an external file. I've tried many things including

from GUI import root, win
root.destroy()
win.destroy()

and

from GUI import root, win, gui_kill
gui_kill()

some produce errors, some don't but none of them kill the windows

7
  • Why do you want to kill the windows? You could terminate stuff by finding the process id of the actual Tkinter gui and kill it that way. Or you could make your window with a listener that listens on some port for a kill signal. Commented Dec 9 at 14:34
  • @JasonChia I have python running a flask web page also, so I can't kill python say from a batch file which I was doing until now. I have heard about the "listener" idea before but I could not get my head around it or get it to work. Since I am a beginner, I figured that using the destroy() method is simple enough and that there might be a way to do this externally. Commented Dec 9 at 14:43
  • @Paxton , Since they are two different processes, you need to take help of OS in order to achieve what you want. The two program don't even know the existence of each other. Commented Dec 9 at 14:49
  • Could you upload a minimal example of your flask app and how you are using the windows? I can't imagine how or why you would need to terminate it externally. Furthermore is it from the client side or server side? Too many unknowns to give you a working solution. I mean you could even just do a post/get route that kills the windows anyways. Commented Dec 9 at 15:06
  • 1
    What do you mean by "external file"? Is that file imported by the main process? Or is that file executed in another process? You need to provide more information and a minimal reproducible example. Commented Dec 9 at 15:14

1 Answer 1

-1

You can use signals or events to communicate between your main script and the external script. For example, you can use a threading event to signal the main script to close the windows.

GUI.py:

import threading
import tkinter as tk

class GUI:
    def __init__(self):
        self.root = tk.Tk()
        self.win = tk.Toplevel()
        self.shutdown_event = threading.Event()

        # Bind a key or button to trigger shutdown
        self.root.bind('<Escape>', lambda event: self.shutdown())

    def shutdown(self):
        self.shutdown_event.set()
        self.root.destroy()
        self.win.destroy()

    def run(self):
        while not self.shutdown_event.is_set():
            self.root.update()
            self.win.update()

# Your GUI code here
gui = GUI()
gui.run()

external_script.py:

from GUI import gui

gui.shutdown()
Sign up to request clarification or add additional context in comments.

3 Comments

Did you notice that from GUI import gui will not return until gui.run() returns? If you press Escape key to terminate the while loop inside gui.run() and then execute gui.shutdown() (inside external_script.py), it will raise exception because the window has been destroyed.
self.win.update() and self.win.destroy() aren't needed. Also calling self.root.destroy() destroys self.win so I am not sure why that wouldn't be raising an error. Also also, there is no point in overcomplicating it with threading.Event when a simple bool will suffice.
this code makes no sense because external file will execute gui.shutdown() after user close (manually) window - so it is too late.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.