【发布时间】:2021-05-06 23:30:41
【问题描述】:
最近我一直在制作一个机器人并做了很多,但就在不久前我遇到了这个错误,我并没有真正解决这个错误。所以我想我可以请你帮忙。
上下文:
对于目前的情况,我的机器人是发送一条我自己发送的自定义消息。为此,我创建了一个 tkinter 窗口,其中有一个Text 小部件和一个Button。我在Text 小部件中输入要发送的消息,然后单击Button 发送消息。机器人必须发送该消息,就像 它 正在发送它一样。
错误:
脚本运行良好,但每当我单击按钮发送文本时。它向我显示了下面提到的错误:
C:\Users\Bhavyadeep\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py:1421: RuntimeWarning: coroutine 'custom_message.<locals>.cm_send' was never awaited
self.tk.mainloop(n)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
我自己尝试了几件事,但似乎都没有。
代码:
这是与此问题相关的代码:
@NucleoTech.command()
async def custom_message(ctx):
if ctx.author.id == myid:
confirm_embed = discord.Embed(title='Custom Message Window Opened!', description='Hey Bhavyadeep! I have opened the custom message window now. You can see it is visible to you now.', color=0x4fff4d)
confirm_embed.set_footer(text=ctx.author)
await ctx.channel.send(embed=confirm_embed)
cm_window = Tk()
cm_window.geometry('300x300')
cm_window.resizable(False, False)
cm_text = Text(cm_window, height=15, width=36)
cm_text.place(x=2.75, y=1)
async def cm_send():
text = cm_text.get(1.0, 'end')
sendcm_embed = discord.Embed(title='Custom Message!', desciption=f'{text}')
sendcm_embed.set_author(name='NucleoBot')
sendcm_embed.set_footer(text=ctx.author)
await ctx.channel.send(embed=sendcm_embed)
await asyncio.sleep(2)
text.delete(1.0, 'end')
cm_send_button = Button(cm_window, text='Send', command=cm_send, width=20, height=2)
cm_send_button.place(x=2, y=250)
cm_window.mainloop()
if ctx.author.id != myid:
errorperm_embed = discord.Embed(title='Access Denied!', description='This command is `OWNER` only. You are not allowed to use this. Try not to execute it another time.', color=0xFF0000)
errorperm_embed.set_footer(text=ctx.author)
await ctx.channel.send(embed=errorperm_embed)
如果您仍然需要完整的代码,您可以随时向我索取。我会发送它。 :)
谢谢! :)
【问题讨论】:
-
我会看线程。将 tkinter 保留在主线程中(因为它不是线程安全的)并在另一个线程中运行机器人。
-
@Henry 感谢您的帮助,但我不明白您的意思。你想让我在另一个文件中运行这个窗口,然后用 Bot 的运行代码将它导入到主文件中?
-
Something like this 可能会有所帮助
标签: python python-3.x tkinter discord.py