Python Forum
“main thread is not in main loop” in Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
“main thread is not in main loop” in Tkinter
I have paths of two GIF files in a list.
After input (name of the planet) it just shows looped GIF in tkinter.
When you enter 'ok' as a second input GIF quits and you can repeat the action.
But the second time GIF doesn't show up and after 'ok' input it reports
“main thread is not in main loop” error.

#! python3

planets = [ 'earth', 'mars' ]

ear = r"C:\Users\User\Desktop\python files\Earth.gif"
mar = r"C:\Users\User\Desktop\python files\Mars.gif"

GIF = [ ear, mar ]

def main():
        print('Enter the name of the planet you like to see')
        inpuT = input()

        while inpuT not in planets:
                print('Enter the name of the planet you like to see')
                inpuT = input()

        a = planets.index(inpuT)

        import tkinter as tk
        from tkinter import PhotoImage                                              
        from tkinter import Label
        import threading
        from PIL import Image, ImageTk

        class AnimatedGIF(Label, object):
            def __init__(self, master, path, forever=True):
                self._master = master
                self._loc = 0
                self._forever = forever

                self._is_running = False

                im =
                self._frames = []
                i = 0
                    while True:
                        photoframe = ImageTk.PhotoImage(im.copy().convert('RGBA'))

                        i += 1
                except EOFError: pass

                self._last_index = len(self._frames) - 1

                    self._delay =['duration']
                    self._delay = 100

                super(AnimatedGIF, self).__init__(master, image=self._frames[0])

            def start_animation(self, frame=None):
                if self._is_running: return

                if frame is not None:
                    self._loc = 0

                self._master.after(self._delay, self._animate_GIF)

            def _animate_GIF(self):
                self._loc += 1

                if self._loc == self._last_index:
                    if self._forever:
                        self._loc = 0
                        self._callback_id = self._master.after(self._delay, self._animate_GIF)

                    self._callback_id = self._master.after(100, self._animate_GIF)   

            def pack(self, start_animation=True, **kwargs):
                if start_animation:

                super(AnimatedGIF, self).pack(**kwargs)

        class Worker(threading.Thread):                    
                def run(self):
                        global root                        
                        root = tk.Tk()                     
                        win = AnimatedGIF(root, GIF[a])    

        w = Worker()                                       
        inpuT_1 = input()
        if inpuT_1 == 'ok':

while True:
Exception in thread Thread-2: Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\", line 917, in _bootstrap_inner File "C:\Python33\vezbe\GIF-treding problem", line 111, in run z.put(AnimatedGIF(root, alt[a])) File "C:\Python33\vezbe\GIF-treding problem", line 60, in __init__ photoframe = ImageTk.PhotoImage(im.copy().convert('RGBA')) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\", line 117, in __init__ self.__photo = tkinter.PhotoImage(**kw) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\tkinter\", line 3545, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\tkinter\", line 3501, in __init__'image', 'create', imgtype, name,) + options) RuntimeError: main thread is not in main loop
AnimatedGIF class code i downloaded from but i found that only half of it works fine for the GIF to go loop so i chopped part of it.

Only one random gif file and it's file path is enough to reproduce the error.
I have limited knowledge about threading and queueing so any help/tip/notion is highly appreciated.
possible reasoning to that error

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How to clip layout to sides and bottom of main window? Valmont 9 564 Mar-24-2021, 10:00 PM
Last Post: deanhystad
  [Kivy] Kivy pop up shows duplicate buttons from main screen CEKinch 0 524 Feb-05-2021, 05:40 PM
Last Post: CEKinch
  "tkinter.TclError: NULL main window" Rama02 1 677 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  [Tkinter] showing return from button on main screen blazejwiecha 4 623 Nov-22-2020, 04:33 PM
Last Post: blazejwiecha
  [Tkinter] Hi, Keep postition of main window after iconify() delphinis 3 775 Jul-12-2020, 06:59 AM
Last Post: DT2000
Question [Tkinter] Defining a self made module's function to interact with the main .py variables? Gilush 9 1,833 Jun-08-2020, 09:08 AM
Last Post: Gilush
  changing tkinter label from thread nanok66 3 1,725 Jun-07-2020, 01:37 AM
Last Post: nanok66
  [Tkinter] Auto re-fit frames sizes in main window Gilush 2 776 Jun-06-2020, 03:14 AM
Last Post: Gilush
  [Tkinter] How to add multiple frames to main window Dandy_Don 13 2,646 Apr-29-2020, 09:21 PM
Last Post: Dandy_Don
  Active tkinter text output during loop dvanommen 2 5,086 Oct-18-2019, 02:23 PM
Last Post: dvanommen

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020