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
Reply


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,087 Oct-18-2019, 02:23 PM
Last Post: dvanommen

Forum Jump:

User Panel Messages

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