无法停止该程序

时间:2018-05-29 17:12:27

标签: python tkinter

我有一个我已经制作的spambot的代码,但我不知道如何更改运行的布尔值,以便在我按下按钮时启动和停止代码。在我输入输入中的第一个字母后它也会冻结,但程序仍在后台运行。

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

0 个答案:

没有答案