首页 > 解决方案 > 无法停止程序

问题描述

我有我制作的垃圾邮件机器人的代码,但我不知道如何更改正在运行的布尔值,以便在我按下按钮时启动和停止代码。在我输入第一个字母后它也会冻结,但程序仍然在后台运行。

from tkinter import *
from pynput.keyboard import Key, Controller
import time

    keyboard = Controller()

running = True
idx = 0

def start():
    global running
    running = True

def stop():
    global running
    running = False

root = Tk()

var = StringVar()

root.geometry("500x300")
root.title("Spambot")

write = Label(text="Insert sentence then print the button:")
button = Button(text="Spam", command=start)
stop = Button(text="Stop", command=stop)
enter = Entry(root, textvariable = var) 

write.pack()
    enter.pack()
    button.pack()
    stop.pack()

time.sleep(2)

while True:

    if idx % 500 == 0:
        root.update()

    if running:

        for char in var.get():
            keyboard.press(char)
            keyboard.release(char)
            time.sleep(1)
        idx += 1

root.mainloop()

标签: pythontkinter

解决方案


推荐阅读