如明境 发表于 2022-9-19 09:05:00

nonebot与tkinter之间怎么写

想写一个有界面的qq机器人插件,但是出现了以下几个问题。
1.用函数返回值方法调用gid列表,但是因为用了nonebot官方的事件通知和装饰器提示无法参数错误:TypeError: qlb() missing 2 required positional arguments: 'bot' and 'event'
import nonebot,asyncio
from nonebot.adapters.onebot.v11 import GroupIncreaseNoticeEvent,Bot,Adapter,LifecycleMetaEvent
from nonebot import on_metaevent,on_notice
from tkinter import *

nonebot.init()
driver = nonebot.get_driver()
driver.register_adapter(Adapter)

# 实例一个元事件响应器
ysj = on_metaevent()
# 获取群列表
@ysj.handle()
async def qlb(bot:Bot,event:LifecycleMetaEvent):
    glds = await bot.get_group_list()
    gid = []
    for gld in glds:
      gid.append(str(gld['group_id']))
    return gid

chko = Tk()
chko.geometry('850x680')# 设置窗口大小
# 创建一个空列表框
qcylb = Listbox(chko)
qcylb.pack()
# 在列表框添加数据
gid = asyncio.run(qlb())
for qcyk in gid:
    qcylb.insert(END,qcyk)
mainloop()

if __name__ == "__main__":
    nonebot.logger.warning("Always use `nb run` to start the bot instead of manually running!")
    nonebot.run()
2.我又尝试了global gid 定义函数变量到全局,但在运行时会报错提示gid未定义:NameError: name 'gid' is not defined
import nonebot,asyncio
from nonebot.adapters.onebot.v11 import GroupIncreaseNoticeEvent,Bot,Adapter,LifecycleMetaEvent
from nonebot import on_metaevent,on_notice
from tkinter import *

nonebot.init()
driver = nonebot.get_driver()
driver.register_adapter(Adapter)

# 实例一个元事件响应器
ysj = on_metaevent()
# 获取群列表
@ysj.handle()
async def qlb(bot:Bot,event:LifecycleMetaEvent):
    glds = await bot.get_group_list()
    global gid
    gid = []
    for gld in glds:
      gid.append(str(gld['group_id']))

chko = Tk()
chko.geometry('850x680')# 设置窗口大小
# 创建一个空列表框
qcylb = Listbox(chko)
qcylb.pack()
# 在列表框添加数据
for qcyk in gid:
    qcylb.insert(END,qcyk)
mainloop()

if __name__ == "__main__":
    nonebot.logger.warning("Always use `nb run` to start the bot instead of manually running!")
    nonebot.run()
3.最后我尝试了把窗口部分定义为函数,然后在获取到gid时调用窗口函数,这样虽然成功在列表框添加了数据,但是却卡在这里不在执行后面代码,必须关闭窗口后在往下执行
import nonebot,asyncio
from nonebot.adapters.onebot.v11 import GroupIncreaseNoticeEvent,Bot,Adapter,LifecycleMetaEvent
from nonebot import on_metaevent,on_notice
from tkinter import *

nonebot.init()
driver = nonebot.get_driver()
driver.register_adapter(Adapter)

# 实例一个元事件响应器
ysj = on_metaevent()
# 获取群列表
@ysj.handle()
async def qlb(bot:Bot,event:LifecycleMetaEvent):
    glds = await bot.get_group_list()
    global gid
    gid = []
    for gld in glds:
      gid.append(str(gld['group_id']))
    tkck(gid)
def tkck(gid):
    chko = Tk()
    chko.geometry('850x680')# 设置窗口大小
    # 创建一个空列表框
    qcylb = Listbox(chko)
    qcylb.pack()
    # 在列表框添加数据
    for qcyk in gid:
      qcylb.insert(END,qcyk)
    mainloop()
tzsj = on_notice()
# 群通知监听
@tzsj.handle()
async def qzd(bot: Bot, event: GroupIncreaseNoticeEvent):
    if str(event.group_id) in gid2:
      uid = str(event.user_id)
      print(uid)
   
if __name__ == "__main__":
    nonebot.logger.warning("Always use `nb run` to start the bot instead of manually running!")
    nonebot.run()大佬们,我该怎么办

如明境 发表于 2022-9-19 20:30:33

有没有懂这个的大佬解答下
页: [1]
查看完整版本: nonebot与tkinter之间怎么写