0

I am getting this error when I am trying to open another window from the main window tkinter window, first time it is running perfectly and also performing tasks also but after 2 times it gives this error and whole program gets crashed and get closed down without any warning , I don't know that how to handle this error in python if there is any error handling technique for this please help me on this , in my case I am just calling the other tkinter window from main tkinter window ,I have tried very hard to solve this error but it's not getting resolved as it is coming again and again , tried all the methods given in previous posts but still it's coming , I know that tkinter is not thread safe but how to handle it any ideas, I am new to this ?

root=Tk(mt_debug=1)
root.geometry('454x567')
B=Button(root,text='Plot window',command=lambda: func3(parameter)).grid(row=1,column=2,padx=10,pady=10)
root.mainloop()

def func3(parameter):
  threading.Thread(target=lambda: Plottingandselect(parameter)).start()
  #using threading to call  the
  #another window due to which above error is coming after opening and
  #closing it 2-3 times

def Plottingandselect(rollno):
      window=Tk(mt_debug=1)
      window.title("Marks Distribution")
      Label(window, text=rollno).grid(row=1,column=2)

      Label(window,text="X axis").grid(row=2,column=1)
      Label(window, text="Marks",relief=SUNKEN).grid(row=3, column=1)
      Label(window,text="Y axis").grid(row=2,column=3,padx=22)
      OPTIONS1 = [
        "Physics",
        "Maths",
        "Chemistry",
        "Biology",
      ]
      list1 = Listbox(window, selectmode="multiple", relief=SUNKEN, font=("Times New Roman", 10))
  #then user will select above parameters and graphs will be plotted and 
  #it is plotting also perfectly multiple times also , but when i am closing
 # this plotting window and again I select another roll number and do the 
 #same 2-3 times it gives the following error
 # mt_debug I am using because I thought that mttkinter will handle it but it's not true

This is the error:

 invalid command name "233512792_check_events"
 while executing
"233512792_check_events"
("after" script)
Exception in Tkinter callback
Tcl_AsyncDelete: async handler deleted by the wrong thread 

Tried many methods, now having no idea how to resolve it.

martineau
  • 119,623
  • 25
  • 170
  • 301
Ankur
  • 457
  • 6
  • 21
  • There may be issue running `Tk()` in child thread. – acw1668 Jun 19 '20 at 02:21
  • `tkinter` doesn't support multithreading. This means only **one** thread can use it. You _can_ use threads, but the others will have to communicate with the one running the GUI though a `Queue` or some other mechanism like `threading.Semaphore` or `threading.Condition` objects. There's a universal `tkinter` widget method named [`after()`](https://web.archive.org/web/20190222214221id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) which can be used to schedule periodic calls to a function to check the status of other non-GUI thread(s). – martineau Jun 19 '20 at 02:22
  • @martineau can you give a example please how to program it ? can you give any example?please – Ankur Jun 19 '20 at 02:27
  • @acw1668 so according to you i should not call threading for plottingandselect ?Because when i am not using threading for it my tkinter window is getting hanged that's why I have used threading – Ankur Jun 19 '20 at 02:28
  • Yes, see [Freezing/Hanging tkinter Gui in waiting for the thread to complete](https://stackoverflow.com/questions/53696888/freezing-hanging-tkinter-gui-in-waiting-for-the-thread-to-complete) for an example. – martineau Jun 19 '20 at 02:32
  • @martineau you mentioned 'threading.Semaphore' can you explain me on that by relating to my example and please can you write a elaborate answer on it if possible ? – Ankur Jun 19 '20 at 02:37
  • 1
    Try using `Toplevel` instead of `Tk` inside `Plottingandselect()` function and without using thread. – acw1668 Jun 19 '20 at 02:42
  • Sorry, I personally have never used one for this, so have no firsthand knowledge. Semaphores are used to control access to a common resource (like data-structure or even a whole GUI), so I mentioned them mostly from a theoretical perspective. Maybe @acw1668's suggestion of using [`Toplevel`](https://web.archive.org/web/20190429194251id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/toplevel.html) to create another window and avoiding threads would be simpler. In general calling `Tk()` more than once in a `tkinter` app is going to cause problems. – martineau Jun 19 '20 at 02:51
  • @acw1668 you are just fabulous amazing it solved my problem – Ankur Jun 19 '20 at 02:55
  • Please post it as your answer the amazing developer @acw1668 – Ankur Jun 19 '20 at 02:55

1 Answers1

1

For your case, you can simply use Toplevel instead of Tk inside Plottingandselect() function and no threading is required:

def Plottingandselect(rollno):
    window = Toplevel()
    window.title("Marks Distribution")
    ...

...
root = Tk(mt_debug=1)  # are you using mtTkinter?
root.geometry('454x567')
B = Button(root, text='Plot window', command=lambda: Plottingandselect(parameter)) # 'parameter' is not defined in your original code
B.grid(row=1, column=2, padx=10, pady=10)
root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Can we call generate plot in main window without the help of threading , I am not using any tkinter window but directly plotting matplotlib plot , but I need threading for it because it gets hanged – Ankur Jun 19 '20 at 03:39
  • 1
    Do you want to do [this](https://matplotlib.org/3.2.1/gallery/user_interfaces/embedding_in_tk_sgskip.html)? – acw1668 Jun 19 '20 at 04:42
  • one question I want to ask you that I have in mu GUI two plot buttons but I am not able to plot them at the same time means tkinter windows gets hanged when I call two matplotlib window at the same time so embedding them the matplotlib graph in tkinter will help? please help on this? – Ankur Jun 19 '20 at 04:48
  • @Ankur You should create another SO question so that we can help. – acw1668 Jun 19 '20 at 04:51
  • I posted please see @acw1668 – Ankur Jun 19 '20 at 05:25