如何让 tkinter 与discord.py 一起工作?

发布于 2025-01-12 20:36:56 字数 1403 浏览 3 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2025-01-19 20:36:56

不幸的是,您无法使用按钮将机器人和 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

染柒℉ 2025-01-19 20:36:56

您可以将它们作为单独的文件,并创建一个运行它们的批处理文件,但是我无论如何也想不出允许 Tkinter 输入在不使用 import 的情况下与不和谐机器人进行通信,无论快速批处理脚本,例如...

start "tkinter file path"
start "discord bot file path"
exit

...将解决最初的问题

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...

start "tkinter file path"
start "discord bot file path"
exit

...would solve the initial problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文