本帖最后由 hrp 于 2020-12-12 10:17 编辑
把时间间隔调小就移动平滑了,我刚改成同一用一个全局变量控制时间间隔了import tkinter as tk
from threading import Thread
from time import sleep
import pyautogui as ag
# 不生效的原因是KEEP_LISTEN = False忘记改True了我的天
KEEP_LISTEN = True
THREAD_LISTENING = None
POSITION = 0, 0
# 刷新时间间隔(毫秒)
TIME_INTERVAL = 1
def listening():
'''监听函数'''
global POSITION, KEEP_LISTEN
KEEP_LISTEN = True
while KEEP_LISTEN:
new_position = ag.position()
if new_position != POSITION:
POSITION = new_position
sleep(TIME_INTERVAL / 1000)
def start_listen():
'''调用此函数开始监听'''
global THREAD_LISTENING
if THREAD_LISTENING:
return
THREAD_LISTENING = Thread(target=listening)
THREAD_LISTENING.start()
def stop_listen():
'''调用此函数停止监听'''
global KEEP_LISTEN, THREAD_LISTENING
KEEP_LISTEN = False
THREAD_LISTENING.join()
THREAD_LISTENING = None
root = tk.Tk()
root.attributes("-alpha", 0.6)
root.overrideredirect(True)
etyText = tk.StringVar()
def mouse_move_event():
# POSITION[0] + 20 原因是使输入框离鼠标远点防止影响鼠标点击,实际使用可以改回来
root.geometry(f'80x20+{POSITION[0] + 20}+{POSITION[1]}')
etyText.set(f'{POSITION[0]},{POSITION[1]}')
root.after(TIME_INTERVAL, mouse_move_event)
ety = tk.Entry(root, textvariable=etyText)
ety.pack()
start_listen()
root.after(TIME_INTERVAL, mouse_move_event)
tk.mainloop()
|