0

I was trying to implement a GUI for an existing program that was controlled through the Keyboard. So I started with using Tkinter.

However, as I was searching and trying for solutions, I realized that in order for my Tkinter GUI program needs to run along side my original program I need multi-threading. Since I did not want to change too much of the original program, risking creating more complicated bugs, I tried to run the program on the main thread and GUI on a created thread.

As a result, I kept getting this error

Tcl_AsyncDelete: async handler deleted by the wrong thread

Aborted (core dumped)

And this is my tester code:

from Tkinter import *
from GUI_interface import *
import threading
import time

class guiThread (threading.Thread):
    def __init__(self, inc):
        threading.Thread.__init__(self)
        self.inc = inc
        self.start()

    def run(self):

        # initializing GUI interface
        self.root = Tk()
        self.root.geometry("600x300+500+500")
        self.myGUI = Example(self.root, self.inc)

        self.root.mainloop()
        self.root.quit()
        self.__del__()

    def __del__(self):
        print "now I'm dead"

def main():
    global_header.init()
    my = guiThread(4)

    while 1:
        time.sleep(2)
        print my
        if not my.isAlive():
            my = None
            break;
        print "%d" % global_header.t

if __name__ == "__main__":
    main()

I read from other sites that it will be better to use the Tkinter thread as the main thread but that would cause many changes to the original program, which I am reluctant to do.

Thanks!

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    tkinter and threads do not mix well, see for example http://stackoverflow.com/questions/10556479/running-a-tkinter-form-in-a-separate-thread – J.J. Hakala Jun 18 '16 at 00:25
  • If you're not averse to switching GUI libraries, threading is relatively simple using `Qt` and `PyQt`/`PySide` – Brendan Abel Jun 18 '16 at 00:31
  • If you have a single threaded interactive program with a text interface, then using tkinter should mean that you end up with a single threaded interactive program with a graphics interface. This means replacing the current text interface part of the program with the graphics interface. How this is done depends on how the program is currently organized. Two things that might help: 1. The builtin `input` function can be replaced by a function that puts prompts in and gets user input from a graphics window. 2. Print takes a `file` argument. Its write method can put text into a graphics box. – Terry Jan Reedy Jun 19 '16 at 00:52

0 Answers0