|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我的想法是如果鼠标移动,就把当前鼠标坐标写入文本框,请问该怎么实现呢?
- import tkinter as tk
- import pyautogui as ag
- root = tk.Tk()
- root.attributes("-alpha", 0.6)
- root.overrideredirect(True)
- etyText = tk.StringVar()
- if ag.position() != (0, 0): # <--------------這裡该怎麼寫?
- xx = ag.position()[0] + 20
- yy = ag.position()[1] + 20
- root.geometry(f'60x20+{xx}+{yy}')
- etyText.set(f'{ag.position()[0]},{ag.position()[1]}')
- ety = tk.Entry(root, textvariable=etyText)
- ety.pack()
- tk.mainloop()
复制代码
本帖最后由 hrp 于 2020-12-11 22:40 编辑
回家一试竟然发现是参数忘改了
- 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
- def listening(time_interval):
- global POSITION
- while KEEP_LISTEN:
- new_position = ag.position()
- if new_position != POSITION:
- POSITION = new_position
- sleep(time_interval)
- def start_listen(t):
- '''调用此函数开始监听'''
- global THREAD_LISTENING
- # t 是刷新时间间隔
- if THREAD_LISTENING:
- return
- THREAD_LISTENING = Thread(target=listening, args=(t,))
- 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(100, mouse_move_event)
- ety = tk.Entry(root, textvariable=etyText)
- ety.pack()
- # 刷新间隔0.05秒
- start_listen(0.05)
- root.after(50, mouse_move_event)
- tk.mainloop()
复制代码
|
|