英文:
PySimpleGui throwing error when i try to login my bot
问题
我正在为一个应用程序编写一个机器人管理器,并且一直收到这个错误,我没有使用线程库,也没有使用工作线程:
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: 主线程不在主循环中`
这个函数:
def emit(self, record):
# 获取当前日志
log_msg = self.ui_element.get()
# 格式化日志记录并追加
log_msg += self.format(record)
print(record)
# 使用日志消息更新UI元素
self.ui_element.update(log_msg)
ui_handler = UiLogHandler(window["logs"])
logging.getLogger().addHandler(ui_handler)
英文:
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)
答案1
得分: 1
方法emit
被调用时不在主循环或主线程中,因此不要在此方法中更新GUI元素,而应该使用Window.write_event_value
方法生成一个事件来在事件循环中更新您的GUI元素,例如
def emit(self, record):
# 获取当前日志
log_msg = self.ui_element.get()
# 格式化日志记录并追加
log_msg += self.format(record)
print(record)
# 使用日志消息更新UI元素
self.window.write_event_value('Update', log_msg)
在您的事件循环中
if event == 'Update':
log_msg = values[event]
ui_element.update(log_msg)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论