python - 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 已被编辑。
解决方案
推荐阅读
- collections - 如何转换列表
到地图 使用 Java8 流 - pandas - 防止 pandas 插值外推
- git - 本地和远程文件的 git 2 版本
- css - 使 div 下的 div 与父级宽度相同,即使其子级大于其父级
- ssl - 通过非 HTTP 协议代理的 TLS 终止(加密/解密)
- c++ - C++ 自注册类成员
- javascript - 我有一个使用 vuetify 的 vue 模板,我想在模板中安装 nuxt js
- reactjs - React Context 和 Apollo Client Link 之间的通信
- c++ - C ++ 14函数返回对象数组,尽管复制构造函数被删除
- reactjs - 使用 React + Material UI 创建自定义国家/地区选择