點擊獲取后,返回2s后的鼠標位置,顯示在文本框
(需要用pip命令安裝所需的的庫)
(pip install 模塊名
比如 安裝pyautogui 模塊
在cmd里面輸入: pip install pyautogui)
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import time
import pyautogui as pag
import tkinter
def get():
po.delete(0,tkinter.END)
time.sleep(2) #幾秒后返回位置
x , y = pag.position()
po.insert(0,str(x)+','+str(y))
root = tkinter.Tk()
tip = tkinter.Label(root,text="返回點擊獲取2s后的光標位置")
tip.grid(row=0)
po = tkinter.Entry(root)
po.grid(row=1)
do = tkinter.Button(root,text="獲取",command=get) #點擊獲取位置
do.grid(row=2)
root.mainloop()
閑得無聊把時間調成可改了,但是其中一個if莫名其妙運行不了,求大神解釋QAQ
由于布局問題,若要調整窗口位置,請拖動左上角
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import time
import pyautogui as pag
import tkinter
mytime = 2
#判斷時間是否合法
def safe():
text = getmytime.get();
#小數點個數
point = 0
if(text==""):
return False
for i in text:
if(i>='0' and i<='9'and point<2):
continue
elif(i=='.'):
point = point + 1
else:
return False
return True
def get():
global mytime
if(safe()):
mytime = float(getmytime.get())
#不知道為何下面的這個if沒用
if(mytime>7.0):
showpos.delete(0,tkinter.END)
showpos.insert(0,"請耐心等候")
time.sleep(mytime) #幾秒后返回位置
x , y = pag.position()
showpos.delete(0,tkinter.END)
showpos.insert(0,str(x)+','+str(y))
else:
showpos.delete(0,tkinter.END)
showpos.insert(0,"輸入非法喲~")
root = tkinter.Tk()
root.resizable(0,0)
tip1 = tkinter.Label(root,text="點擊按鈕獲取")
tip1.place(relx=0.1,rely=0.1)
getmytime = tkinter.Entry(root,width=3)
getmytime.place(relx=0.6,rely=0.1)
getmytime.insert(0,str(mytime))
tip2 = tkinter.Label(root,text="s后的")
tip2.place(relx=0.8,rely=0.1)
tip3 = tkinter.Label(root,text="光標位置:")
tip3.place(relx=0.1,rely=0.3)
showpos = tkinter.Entry(root,width=10)
showpos.place(relx=0.5,rely=0.3)
do = tkinter.Button(root,text="按鈕",command=get) #點擊獲取位置
do.place(relx=0.8,rely=0.6)
root.mainloop()
以上這篇用python3 返回鼠標位置的實現方法(帶界面)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

