基于python實(shí)現(xiàn)鼠標(biāo)實(shí)時(shí)坐標(biāo)監(jiān)測(cè)
python實(shí)現(xiàn)鼠標(biāo)實(shí)時(shí)坐標(biāo)監(jiān)測(cè)
一、說(shuō)明
使用了以下技術(shù)和庫(kù):
- tkinter:用于創(chuàng)建GUI界面。
- pyperclip:用于復(fù)制文本到剪貼板。
- pynput.mouse:用于監(jiān)聽(tīng)鼠標(biāo)事件,包括移動(dòng)和點(diǎn)擊。
- threading:用于創(chuàng)建多線程,以便在后臺(tái)執(zhí)行鼠標(biāo)事件監(jiān)聽(tīng)和標(biāo)簽更新的任務(wù)。
- time:用于控制線程休眠,以定時(shí)更新標(biāo)簽文本。
二、代碼
# coding=gbk # 指定文件編碼為GBK import tkinter as tk # 導(dǎo)入tkinter庫(kù),用于創(chuàng)建GUI界面 import pyperclip # 導(dǎo)入pyperclip庫(kù),用于復(fù)制文本到剪貼板 from pynput import mouse # 導(dǎo)入pynput庫(kù)的mouse模塊,用于監(jiān)聽(tīng)鼠標(biāo)事件 import threading # 導(dǎo)入threading庫(kù),用于創(chuàng)建多線程 import time # 導(dǎo)入time庫(kù),用于線程休眠 # 創(chuàng)建一個(gè)MouseCoordinateApp類(lèi),用于處理鼠標(biāo)坐標(biāo)顯示和復(fù)制 class MouseCoordinateApp: def __init__(self): self.root = tk.Tk() # 創(chuàng)建一個(gè)Tkinter窗口 self.root.title("鼠標(biāo)坐標(biāo)實(shí)時(shí)展示") # 設(shè)置窗口標(biāo)題 self.root.geometry("350x80") # 設(shè)置窗口大小 self.root.resizable(False, False) # 禁止窗口大小調(diào)整 self.label = tk.Label(self.root, text="單機(jī)截取坐標(biāo):X: - , Y: -\n實(shí)時(shí)坐標(biāo):X: - , Y: -") # 創(chuàng)建一個(gè)標(biāo)簽控件 self.label.pack() # 將標(biāo)簽控件添加到窗口 copy_button = tk.Button(self.root, text="復(fù)制坐標(biāo)", command=self.copy_coordinates) # 創(chuàng)建一個(gè)按鈕控件 copy_button.pack() # 將按鈕控件添加到窗口 self.root.attributes("-topmost", True) # 設(shè)置窗口置頂 self.extracted_coordinates = (0, 0) # 初始化截取坐標(biāo) self.current_coordinates = (0, 0) # 初始化實(shí)時(shí)坐標(biāo) self.last_extracted_coordinates = (0, 0) # 初始化上一次截取的坐標(biāo) self.update_interval = 0.1 # 更新標(biāo)簽的時(shí)間間隔 threading.Thread(target=self.start_mouse_listener, daemon=True).start() # 創(chuàng)建并啟動(dòng)鼠標(biāo)事件監(jiān)聽(tīng)的線程 threading.Thread(target=self.update_label_thread, daemon=True).start() # 創(chuàng)建并啟動(dòng)標(biāo)簽更新的線程 def copy_coordinates(self): x, y = self.last_extracted_coordinates # 獲取上一次截取的坐標(biāo) coordinates = f"X: {x}, Y: {y}" # 格式化坐標(biāo)文本 pyperclip.copy(coordinates) # 復(fù)制坐標(biāo)文本到剪貼板 self.label.config(text=f"已復(fù)制坐標(biāo):{coordinates}") # 更新標(biāo)簽文本 def start_mouse_listener(self): with mouse.Listener(on_move=self.on_move, on_click=self.on_click) as listener: listener.join() # 啟動(dòng)鼠標(biāo)事件監(jiān)聽(tīng) def on_move(self, x, y): self.current_coordinates = (x, y) # 更新實(shí)時(shí)坐標(biāo) def on_click(self, x, y, button, pressed): if pressed: self.last_extracted_coordinates = self.extracted_coordinates # 更新上一次截取的坐標(biāo) self.extracted_coordinates = (x, y) # 如果鼠標(biāo)被按下,更新截取坐標(biāo) def update_label_thread(self): while True: time.sleep(self.update_interval) # 線程休眠一段時(shí)間 self.update_label() # 更新標(biāo)簽文本 def update_label(self): extracted_x, extracted_y = self.extracted_coordinates # 獲取截取坐標(biāo) current_x, current_y = self.current_coordinates # 獲取實(shí)時(shí)坐標(biāo) self.label.config(text=f"截取坐標(biāo):X: {extracted_x}, Y: {extracted_y}\n實(shí)時(shí)坐標(biāo):X: {current_x}, Y: {current_y}") def run(self): self.root.mainloop() # 啟動(dòng)主程序的主循環(huán) if __name__ == "__main__": app = MouseCoordinateApp() # 創(chuàng)建MouseCoordinateApp實(shí)例 app.run() # 啟動(dòng)應(yīng)用程序的主循環(huán)
三、效果
到此這篇關(guān)于基于python實(shí)現(xiàn)鼠標(biāo)實(shí)時(shí)坐標(biāo)監(jiān)測(cè)的文章就介紹到這了,更多相關(guān)python鼠標(biāo)坐標(biāo)監(jiān)測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python點(diǎn)擊鼠標(biāo)獲取坐標(biāo)(Graphics)
- python 讀取鼠標(biāo)點(diǎn)擊坐標(biāo)的實(shí)例
- Python鼠標(biāo)事件及坐標(biāo)獲取窗口和屏幕坐標(biāo)
- python opencv鼠標(biāo)事件實(shí)現(xiàn)畫(huà)框圈定目標(biāo)獲取坐標(biāo)信息
- Python2.7:使用Pyhook模塊監(jiān)聽(tīng)鼠標(biāo)鍵盤(pán)事件-獲取坐標(biāo)實(shí)例
- python 五子棋如何獲得鼠標(biāo)點(diǎn)擊坐標(biāo)
- 一文詳解如何使用Python實(shí)時(shí)輸出鼠標(biāo)坐標(biāo)
相關(guān)文章
Python3.7實(shí)現(xiàn)驗(yàn)證碼登錄方式代碼實(shí)例
這篇文章主要介紹了Python3.7實(shí)現(xiàn)驗(yàn)證碼登錄方式代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02一篇文章搞懂Python反斜杠的相關(guān)問(wèn)題
這篇文章主要給大家介紹了如何通過(guò)一篇文章搞懂Python反斜杠的相關(guān)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Python WEB應(yīng)用部署的實(shí)現(xiàn)方法
這篇文章主要介紹了Python WEB應(yīng)用部署的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事項(xiàng)
這篇文章主要介紹了Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事項(xiàng),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11Python一行代碼對(duì)話ChatGPT實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Python一行代碼對(duì)話ChatGPT實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03python_opencv用線段畫(huà)封閉矩形的實(shí)例
今天小編就為大家分享一篇python_opencv用線段畫(huà)封閉矩形的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12