python - 无法停止程序
问题描述
我有我制作的垃圾邮件机器人的代码,但我不知道如何更改正在运行的布尔值,以便在我按下按钮时启动和停止代码。在我输入第一个字母后它也会冻结,但程序仍然在后台运行。
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()
解决方案
推荐阅读
- flutter - 颤振应用程序 apk 给出 clipboard_manager 错误
- visual-studio-code - 如何在 vscode 中更改 Hover 字体系列
- c++ - 是否需要关闭 fstream ( I/O )?
- kiwi-tcms - Kiwi TCMS 和 Jenkins 集成
- javascript - 显示在 iframe 内的自定义贝宝结帐按钮
- apache-kafka - 两个独立的引导服务器可以订阅同一个 Kafka 主题吗?
- node.js - 吉普错误!堆栈错误:无法建立隧道套接字,原因=在建立安全 TLS 连接之前已断开客户端网络套接字
- python - Selenium get(url) 显示错误的页面
- node.js - 是否可以在 .eleventy 配置文件中使用“addGlobalData”?
- nginx - 从 Plex 服务器 URL 中删除 /web/index.html#!/