我用python做了个连点器,但是最后在关闭窗口时却无法退出进程。
代码如下:
from time import sleep
from tkinter import Tk, Label, Radiobutton, StringVar, Entry
from sys import exit
from keyboard import add_hotkey
from threading import Thread
from pyautogui import click
def Click():
global Start
global Mode
global IWillQuit
global Entry1
while True:
if IWillQuit:
break
if Start:
click(button = Mode.get())
sleep(int(Entry1.get())/1000)
def Turn():
global Start
Start = not Start
sleep(0.3)
def Main():
global root
global Mode
global IWillQuit
Label1 = Label(root, text = "点击类型: ")
Label1.grid(row = 1, column = 1)
Button1 = Radiobutton(root, text="鼠标左键", variable = Mode ,value = "Left")
Button1.grid(row = 1, column = 2)
Button2 = Radiobutton(root, text="鼠标中键", variable = Mode ,value = "Middle")
Button2.grid(row = 1, column = 3)
Button3 = Radiobutton(root, text="鼠标右键", variable = Mode ,value = "Right")
Button3.grid(row = 1, column = 4)
Label2 = Label(root, text = "间隔时间: ")
Label2.grid(row = 2, column = 1)
Label3 = Label(root, text = "ms")
Label3.grid(row = 2, column = 4)
Label4 = Label(root, text = "启停的热键: Ctrl+P")
Label4.grid(row = 3, column = 1)
def Quit():
global T1, T2
global root
IWillQuit = True
root.destroy()
T1.join()
T2.join()
exit()
Version = "0.0.0.1"
Start = False
IWillQuit = False
ww = 700
wh = 300
root = Tk()
root.title(f"连点器 {Version}")
sw, sh = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry(f"{ww}x{wh}+{(sw-ww)//2}+{(sh-wh)//2}")
root.resizable(False, False)
Mode=StringVar()
Mode.set("Left")
Entry1 = Entry(root)
Entry1.grid(row = 2, column = 2, columnspan = 3)
add_hotkey("ctrl+p", Turn)
T1 = Thread(target = Main)
T2 = Thread(target = Click)
T1.start()
T2.start()
Main()
root.protocol("WM_DELETE_WINDOW", Quit)
root.mainloop()
最后在Quit函数时,总是无法关闭进程,有没有人知道该怎么办?
如果您能回复我这个问题,我将万分感谢。