PySimpleGui在我尝试登录我的机器人时抛出错误。

huangapple go评论63阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年1月9日 11:00:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定