0

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()
acw1668
  • 40,144
  • 5
  • 22
  • 34

2 Answers2

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

GremGrem
  • 58
  • 7
  • PS. Your best bet is to either find a different GUI module not Tkinter OR make it a TUI instead and use pyinstaller to compile your py file to exe – GremGrem Apr 01 '22 at 20:51
0

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