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()
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)
Perhaps root = Tk()
should be root = tkinter.Tk()
?
Just try to do:
import tkinter
Instead of
from tkinter import *
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.