'Tkinter Button That Stops Thread
I'm trying to make a discord bot that I can start and stop using tkinter. I have a tkinter button that runs a thread which turns on the bot and since I have a start button, I wanted to make a tkinter button that stops the thread (turning off the bot) but doesn't exit the whole entire program. Though I'm not sure how to stop the thread. I tried to use events and Daemon threads but im pretty sure I coded it wrong as I was faced with a ton of error messages.
import tkinter as tk
from tkinter import filedialog, Text
import os
from PIL import ImageTk,Image
from axelbot import axel_bot
import threading
import multiprocessing
root = tk.Tk()
#title
root.title("Axel")
#icon
root.iconbitmap(r'caticon.ico')
#bg color
root.configure(bg='#292323')
#resize image
axel = Image.open("axel.jpg")
resized = axel.resize((400,400), Image.ANTIALIAS)
resized_axel = ImageTk.PhotoImage(resized)
stop_event = threading.Event()
#image
my_label = tk.Label(root, image=resized_axel,)
my_label.pack()
#open file
t1 = threading.Thread(target=axel_bot).start
start = tk.Button(root, text="Start", font=('Helvetica 8 bold'), command=t1, padx=20, pady=5,fg="White", bg="#292323")
start.pack()
#stop button
button_quit = tk.Button(root, text="Stop", font=('Helvetica 8 bold'), command=exit, padx=20, pady=5, fg="White", bg="#292323")
button_quit.pack()
root.mainloop()
This is what the thread is running:
import discord
import webbrowser
import time
import keyboard
import pyautogui
import threading
from selenium import webdriver
client = discord.Client()
stop_event = threading.Event()
def axel_bot():
#turning axel on
@client.event
async def on_ready():
print('AXEL IS HERE')
axeltoken = ('token')
@client.event
async def on_message(message):
#so it doesnt respond to itself
if message.author == client.user:
return
#poggers
if message.content == 'axel':
await message.channel.send('POGGERS')
if message.content == 'Streamer is live on twitch!':
await message.channel.send("OPENING STREAM!!!")
time.sleep(5)
yassuolink = "https://www.twitch.tv/streamer"
webbrowser.open_new(streamerlink)
pyautogui.hotkey('ctrl', 'w')
await message.channel.send("Closed!")
if message.content == 'edit':
await message.channel.send("googledoc")
if message.content == 'catdance':
catdance = "https://www.youtube.com/watch?v=CbozHK38jDc"
await message.channel.send("Dancing!")
webbrowser.open_new(catdance)
time.sleep(4)
pyautogui.press('f')
client.run(axeltoken)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
