I am writing a bot manager for an app and I keep getting this error, I am not using the threading lib nor a Worker thread:
log_msg = self.ui_element.get()
File "C:\Users\derri\AppData\Local\Programs\Python\Python38\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 3515, in get
value = str(self.TKText.get(1.0, tk.END))
File "C:\Users\derri\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 3706, in get
return self.tk.call(self._w, 'get', index1, index2)
RuntimeError: main thread is not in main loop`
The function:
def emit(self, record):
# Get the current logs
log_msg = self.ui_element.get()
# Format the log record and append
log_msg = self.format(record)
print(record)
# Update the UI element with the log message
self.ui_element.update(log_msg)
ui_handler = UiLogHandler(window["logs"])
logging.getLogger().addHandler(ui_handler)
CodePudding user response:
Method emit
called not in the main loop or main thread, so don't update GUI element in this method, replace it by method Window.write_event_value
to generate an event to update your GUI element in your event loop, like
def emit(self, record):
# Get the current logs
log_msg = self.ui_element.get()
# Format the log record and append
log_msg = self.format(record)
print(record)
# Update the UI element with the log message
self.window.write_event_value('Update', log_msg)
and in your event loop
if event == 'Update':
log_msg = values[event]
ui_element.update(log_msg)