Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: main thread is not in main loop #24

Open
chen9run opened this issue Apr 29, 2023 · 1 comment
Open

RuntimeError: main thread is not in main loop #24

chen9run opened this issue Apr 29, 2023 · 1 comment

Comments

@chen9run
Copy link

help!
Epoch 0: 0%| | 16/38300 [00:12<8:01:53, 1.32it/s, loss=5.03, v_num=7]Exception ignored in: <function Image.del at 0x7f6a3fdf7550>
Traceback (most recent call last):
File "/home/shy/anaconda3/envs/loftr1/lib/python3.8/tkinter/init.py", line 4017, in del
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Epoch 0: 0%| | 38/38300 [00:23<6:37:24, 1.60it/s, loss=5.03, v_num=7]Exception ignored in: <function Variable.del at 0x7f6a3ff42c10>
Traceback (most recent call last):
File "/home/shy/anaconda3/envs/loftr1/lib/python3.8/tkinter/init.py", line 363, in del
if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
RuntimeError: main thread is not in main loop

@Tangshitao
Copy link
Owner

I have not encountered this problem before, but I asked ChatGPT for you. Here is the answer:

The "RuntimeError: main thread is not in main loop" error typically occurs in Python applications that use Tkinter, a GUI library, when certain GUI-related tasks are performed in a non-main thread.

To fix this error, you can do one of the following:

Move the GUI-related tasks to the main thread:
Ensure that all Tkinter-related tasks (e.g., creating, updating, or destroying widgets) are performed in the main thread of your application. In most cases, this means keeping all GUI code within the main body of your script, and not inside functions or methods that are called from other threads.

Use queue.Queue to pass tasks to the main thread:
If you have GUI-related tasks that must be performed in separate threads, you can use a queue.Queue to pass tasks from your secondary threads to the main thread. Here's an example of how to do this:
'import tkinter as tk
import threading
import queue

def update_label():
while True:
text = task_queue.get()
label.config(text=text)
task_queue.task_done()

def threaded_task():
for i in range(10):
task_queue.put(f"Task {i} completed")
time.sleep(1)

root = tk.Tk()

task_queue = queue.Queue()

label = tk.Label(root, text="Waiting for tasks...")
label.pack()

threading.Thread(target=update_label, daemon=True).start()
threading.Thread(target=threaded_task, daemon=True).start()

root.mainloop()'

In this example, the update_label function is responsible for updating a label widget with text sent through the task_queue. The threaded_task function simulates a long-running task that sends text updates to the main thread through the task_queue. The two functions are started in separate daemon threads, and the main thread runs the Tkinter event loop with root.mainloop().

Use a different GUI library that supports multi-threading:
If you find that Tkinter's single-threaded nature is limiting your application, consider using a different GUI library that has better support for multi-threading, such as PyQt or Kivy. These libraries provide more advanced features and can better handle multi-threaded applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants