Jun-26-2019, 02:26 PM
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:
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.
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.