0

I want my script should work in such a way that, close all the previous windows in tkinter.

If I minimise the existing pop up window and again I will run same script, it will again pop up new window. How can I close the already existing window, when i run script again?

Simple code:

import Tkinter as tk
root = tk.Tk()
root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

Maybe if you check all variable in locals and see if you have tk type in it?

import tkinter as tk

root = tk.Tk()

tk_type = type(root)
print(tk_type)
var=0
for var in locals().items():
    if type(var[1]) == tk_type:
      print(var, 'to be deleted')

root.mainloop()

something like this where you can put the test at the begining of the class to see if it exists other TK window object. I'm sure it should exist a better solution though.

ymmx
  • 4,769
  • 5
  • 32
  • 64
  • :Its not working. It suppose work like, if I run script, will popup tk window. I minimise tk window, again i will run same script, it should delete old window and popup new window – Bharat Kathari May 18 '17 at 08:37
  • Ok. I misunderstood then. did you try with the os module http://stackoverflow.com/questions/3054740/terminate-a-python-script-from-another-python-script I think it is the os who will be able to close another python script. The question is how the os can know which scripts (those who have tkinter window) to close. – ymmx May 18 '17 at 08:48
  • Thanks for information. I am not getting how to implement in my same code – Bharat Kathari May 18 '17 at 09:13