停止线程的tkinter按钮

发布于 2025-01-26 18:26:18 字数 2472 浏览 3 评论 0原文

我正在尝试制作一个可以启动并停止使用TKINTER的DISORD机器人。我有一个tkinter按钮,该按钮运行了一个线程,该线程打开了机器人,并且由于我有一个启动按钮,因此我想制作一个停止线程的tkinter按钮(关闭机器人),但不会退出整个程序。尽管我不确定如何停止线程。我尝试使用事件和守护程序线程,但是我可以肯定的是,当我面对大量的错误消息时,我对此进行了编码。

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()


这就是线程正在运行的:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文