我正在尝试制作一个简单的程序来在屏幕上移动光标。
我可以移动光标,但是当我按“ q”时,循环就一直在进行。另外,该程序将停止响应(退出代码-805306369(0xCFFFFFFF)),但是如果我等待,它将再次起作用。我应该在某个地方使用对象吗?我需要一些清理和调试方面的帮助。
from tkinter import *
import autopy
import time
import keyboard
from random import randint
root = Tk()
root.title("Mouse Mover")
root.geometry("400x115")
root.resizable(width=False, height=False)
windowWidth = 400
windowHeight = 115
positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(root.winfo_screenheight()/2 - windowHeight/2)
root.geometry("+{}+{}".format(positionRight, positionDown))
def mouse_mover():
while True:
x = randint(0, 1000) # integers for random location point
y = randint(0, 800) # integers for random location point
autopy.mouse.smooth_move(x, y) # moves mouse
time.sleep(1.5)
if keyboard.is_pressed('q'): # supposed to break loop (it actually doesn't??)
break
button_1 = Button(root, text="Start", font=('Helvetica Neue', 16),
command=mouse_mover) # change state to command=*name of function*
button_1.pack()
label_2 = Label(root, text="press 'q' to stop",
background="gray", font=('Helvetica Neue', 16))
label_2.pack()
root.mainloop()
预期:我应该按gui上的按钮,并且光标应在屏幕上移动。要停止光标移动,请按键盘上的“ q”。
实际::按下按钮,将调用“ mouse_mover”功能,光标会像应有的那样四处移动,但是在经过4或5次移动后停止,并且程序停止响应或移动一次,然后然后我必须再次按下按钮。如果我等待,它最终将开始备份。当我按“ q”时,什么也没有发生,而且还在继续。
编辑:已修复,我将功能更改为
def mouse_mover():
while keyboard.is_pressed('q') == False:
x = randint(0, 1000) # integers for random location point
y = randint(0, 800) # integers for random location point
autopy.mouse.smooth_move(x, y) # moves mouse