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
#1
Greetings
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.

Code:
#! 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 = Image.open(path)
                self._frames = []
                i = 0
                try:
                    while True:
                        photoframe = ImageTk.PhotoImage(im.copy().convert('RGBA'))
                        self._frames.append(photoframe)

                        i += 1
                        im.seek(i)
                except EOFError: pass

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

                try:
                    self._delay = im.info['duration']
                except:
                    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.configure(image=self._frames[frame])

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

            def _animate_GIF(self):
                self._loc += 1
                self.configure(image=self._frames[self._loc])

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

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

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

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


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

        w = Worker()                                       
        w.start()
        inpuT_1 = input()
        if inpuT_1 == 'ok':
                root.quit()

while True:
        main()
Error:
Error:
Exception in thread Thread-2: Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner self.run() File "C:\Python33\vezbe\GIF-treding problem 1.py", line 111, in run z.put(AnimatedGIF(root, alt[a])) File "C:\Python33\vezbe\GIF-treding problem 1.py", 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\ImageTk.py", line 117, in __init__ self.__photo = tkinter.PhotoImage(**kw) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3545, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3501, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) RuntimeError: main thread is not in main loop
AnimatedGIF class code i downloaded from http://code.activestate.com/recipes/5807...mated-gif/ 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.
Reply
#2
possible reasoning to that error
https://stackoverflow.com/questions/1469...p#14695007
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] main GUI is not shown flash77 3 387 Feb-20-2024, 05:27 PM
Last Post: flash77
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,449 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 728 Aug-06-2023, 01:54 AM
Last Post: deanhystad
  [Tkinter] How to set new opened form activate and minimize main from after new for opened? jalal0034 5 1,908 Sep-23-2022, 11:32 AM
Last Post: deanhystad
  main lop in python tkinter barryjo 6 5,058 Jan-24-2022, 03:09 AM
Last Post: deanhystad
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 3,396 Aug-02-2021, 08:47 PM
Last Post: JayCee
  [PyQt] How to clip layout to sides and bottom of main window? Valmont 9 4,838 Mar-24-2021, 10:00 PM
Last Post: deanhystad
  [Kivy] Kivy pop up shows duplicate buttons from main screen CEKinch 0 1,921 Feb-05-2021, 05:40 PM
Last Post: CEKinch
  "tkinter.TclError: NULL main window" Rama02 1 5,784 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  [Tkinter] showing return from button on main screen blazejwiecha 4 2,595 Nov-22-2020, 04:33 PM
Last Post: blazejwiecha

Forum Jump:

User Panel Messages

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