首页 > 解决方案 > Asyncio/Tkinter RuntimeWarning:启用 tracemalloc 以获取对象分配回溯

问题描述

我尝试使用 Tkinter 和 Asyncio 来打开我的一些智能灯。我的计划是使用一个按钮,单击该按钮会使灯变红。但我不断收到此错误:

C:\Users\rcbsa\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py:603: RuntimeWarning: coroutine 'redButton.changeToRed' was never awaited
  _get_default_root('run the main loop').tk.mainloop(n)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

我只写了 8 个月的代码,所以如果我的代码有点乱,我深表歉意,但这里是:

import asyncio, time
from pywizlight import wizlight, PilotBuilder, discovery

import tkinter
from tkinter import *

main_window = tkinter.Tk()
main_window.geometry("600x500+1280+20")
main_window.title("Red")

class redButton:
    async def changeToRed(self):
        await light.turn_on(PilotBuilder(rgb = (255, 0, 0)))
        await light2.turn_on(PilotBuilder(rgb = (255, 0, 0)))
        loop = asyncio.get_event_loop()
        loop.run_until_complete(self.changeToRed(self))
        
    def __init__(self):
        self.light = wizlight([IP REDACTED])
        self.light2 = wizlight([IP REDACTED])        
        self.main_frame = Frame(main_window, width=600, height=500)
        
        self.red_button = Button(self.main_frame,
                                 text = "Red",
                                 command = self.changeToRed)
        
        self.red_button.pack()
        self.main_frame.pack()
    
        tkinter.mainloop()
              
redButton()

我的猜测是它与 changeToRed 函数中的循环变量有关。是的,灯的 IP 已被编辑。

标签: pythontkinterpython-asyncio

解决方案


推荐阅读