Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter doesn't want to work when using discord.py

So my problem is that the tkinter window doesn't appear when I'm using the discord.py API. But when I do ctrl+c to exit out the tkinter window it automatically appears, I think there's a conflict with client.run(TOKEN) because when I take that line out it works, but then my application doesn't do anything.

from tkinter import *
from discord.ext.commands import Bot
from discord import Game

TOKEN = ""
BOT_PREFIX = "!"
client = Bot(command_prefix=BOT_PREFIX)

root = Tk()
root.title("NinjaBoT Discord Client")
root.resizable(0, 0)
root.configure(bg="black")
root.geometry("630x520")
labelframe2 = LabelFrame(root)
labelframe2.config(bg="black", padx=5, pady=10)
labelframe2.grid(row=1, column=3)
labelframe3 = LabelFrame(root)
labelframe3.config(bg="black", padx=5, pady=10)
labelframe3.grid(row=1, column=0)
labelframe = LabelFrame(labelframe3)
labelframe.config(bg="black", padx=5, pady=10)
labelframe.grid(row=2, column=0)
label = Label(root, text="NinjaBoT Discord")
label.config(bg="black", fg="white")
label.config(font=("Courier", 30))
label.grid(row=0)
label1 = Label(labelframe2, text="::USERS::")
label1.config(bg="black", fg="white")
label1.grid(row=0, column=0)
txtscroll = Text(labelframe3)
txtuserlist = Text(labelframe2)
txtmessage = Text(labelframe)

@client.event 
async def on_message(message):
    txtscroll.insert(END, "#" + str(message.channel) + ":" + " " + str(message.author) + ":" + " " + message.content + "\n")
    x = message.server.members
    for users in x:
        txtuserlist.insert(END, str(users.name) + "\n")

@client.event
async def on_ready():
    await client.change_presence(game=Game(name="GUI"))
    txtscroll.insert(END, 'Logged in as' + "\n")
    txtscroll.insert(END, client.user.name + "\n")
    txtscroll.insert(END, client.user.id + "\n")
    txtscroll.insert(END, '-----------------------' + "\n")

scrollbar = Scrollbar(labelframe3)
scrollbar.grid(row=1, column=1, sticky="ns")
txtscroll.config(width=60, height=20, bg="black", fg="white")
txtscroll.grid(row=1, pady=10, sticky=W)
scrollbar.config(bg="black", command=txtscroll.yview)
txtscroll.config(yscrollcommand=scrollbar.set)

txtmessage.config(width=49, height=1)
txtmessage.grid(row=2, column=0, sticky=W)

b = Button(labelframe, text="SEND",fg="white", command="send")
b.grid(row=2, column=1, padx=5)

txtuserlist.config(width=10, height=23)
txtuserlist.grid(row=1, column=0, padx=5, pady=5)

client.run(TOKEN)
root.mainloop()

is there a better way to use client.run(TOKEN) so that this would work? I think is conflicting with root.mainloop()

like image 807
Mike Cash Avatar asked Mar 22 '26 13:03

Mike Cash


2 Answers

I'm not that familiar with Tkinter, but when you run the line client.run(TOKEN), it blocks the code until the bot stops working, essentially not reaching the next line.

You probably want to run root.mainloop() asyncly before you run the bot. You can do that by putting that line in an async function:

async def open_window():
    root.mainloop()

And then adding it to the event loop:

client.loop.create_task(open_window())
client.run(TOKEN)
like image 101
Tom Gringauz Avatar answered Mar 25 '26 00:03

Tom Gringauz


This worked for me: just run bot in another thread: here is an example

import threading
import discord
from tkinter import * 



client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

def run():
  bot.run("bot token")

def an():
    t = threading.Thread(target=run)
    t.start()#run the "run" function on another thread
root = Tk()             
 
root.geometry('100x100')
 
# Create a Button
btn = Button(root, text = 'Run', bd = '5',
                          command = an) 
btn.pack(side = 'top')   
 
root.mainloop()
like image 22
BatteTarte Avatar answered Mar 24 '26 23:03

BatteTarte