'How to make tkinter work with discord.py?
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()
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | GremGrem |