如何让 tkinter 与discord.py 一起工作?
我想使用 tkinter 按钮控制我的 Discord.py 机器人,但是当我将 client.run(token) 放在 tk.mainloop() 上方时,discord.py 机器人会运行,但 tkinter 窗口不显示。当我将 tk.mainloop() 放在 client.run(token) 之上时,tkinter 窗口会显示,但机器人不会运行。有办法解决这个问题吗?如果问题无法解决,是否有任何 gui 框架可以在这种情况下工作?
import discord
from discord.ext import commands
import time
from dhooks import Webhook
from tkinter import *
from PIL import Image, ImageTk
#discord settings
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=".", intents=intents)
token = "" #don't want to leak my token
@client.event
async def on_ready():
await client.change_presence(activity=discord. Activity(type=discord.ActivityType.playing, name='LionWarrior Bot'))
@client.command()
async def create():
guild_id = int(947138903356362842)
channels_count = int(2)
channel_name = "create"
guild = client.get_guild(guild_id)
for channel_spam in range(channels_count):
await guild.create_text_channel(channel_name)
time.sleep(4)
#tkinter settings
tk = Tk()
tk.title("LionWarrior Nuker | Made by LionWarrior")
tk.configure(width=870, height=400)
tk.geometry("870x400")
tk.configure(bg='black')
create_channels = Button(tk, text="Create Channels", command=create, activeforeground="white", activebackground="grey", width=30, height=3)
create_channels.place(x=360, y=50)
client.run(token)
tk.mainloop()
I want to control my discord.py bot with tkinter buttons, but when I place client.run(token) above tk.mainloop() the discord.py bot runs, but the tkinter window doesn't show. When I place tk.mainloop() above client.run(token) the tkinter window shows up, but the bot doesn't run. Is there a way to fix this problem? If the problem can't be fixed, is there any gui framework that can work in this situation?
import discord
from discord.ext import commands
import time
from dhooks import Webhook
from tkinter import *
from PIL import Image, ImageTk
#discord settings
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=".", intents=intents)
token = "" #don't want to leak my token
@client.event
async def on_ready():
await client.change_presence(activity=discord. Activity(type=discord.ActivityType.playing, name='LionWarrior Bot'))
@client.command()
async def create():
guild_id = int(947138903356362842)
channels_count = int(2)
channel_name = "create"
guild = client.get_guild(guild_id)
for channel_spam in range(channels_count):
await guild.create_text_channel(channel_name)
time.sleep(4)
#tkinter settings
tk = Tk()
tk.title("LionWarrior Nuker | Made by LionWarrior")
tk.configure(width=870, height=400)
tk.geometry("870x400")
tk.configure(bg='black')
create_channels = Button(tk, text="Create Channels", command=create, activeforeground="white", activebackground="grey", width=30, height=3)
create_channels.place(x=360, y=50)
client.run(token)
tk.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,您无法使用按钮将机器人和 tkinter 组合在一起,也无法使用 tkinter 的任何类型的输入方法控制机器人。
Mainloop() 和 client.run 发生冲突,并且一次只能有一个处于活动状态。
因此,如果 mainloop() 正在运行,然后您会触发您的机器人。 Client.run 将激活,但 mainloop() 将停止使您的机器人在线,但 tkinter 将停止
Unfortunately you can’t combine the bot and tkinter together using buttons or control the bot using any sort of input methods by tkinter.
Mainloop() and client.run clash and only one can be active at a time.
So if mainloop() is running and then you trigger your bot. Client.run will activate but mainloop() will stop making your bot online but tkinter will stop
您可以将它们作为单独的文件,并创建一个运行它们的批处理文件,但是我无论如何也想不出允许 Tkinter 输入在不使用
import
的情况下与不和谐机器人进行通信,无论快速批处理脚本,例如......将解决最初的问题
You could have them as separate files, and make a batch file that runs both of them, however I can't think of anyway to allow the Tkinter input communicate with the discord bot without using
import
, regardless a quick batch script such as......would solve the initial problem