溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用python3 返回鼠標位置的實現(xiàn)方法(帶界面)

發(fā)布時間:2020-09-29 18:34:32 來源:腳本之家 閱讀:156 作者:叫我彪哥呀 欄目:開發(fā)技術

點擊獲取后,返回2s后的鼠標位置,顯示在文本框

(需要用pip命令安裝所需的的庫)

(pip install 模塊名

比如 安裝pyautogui 模塊

在cmd里面輸入: pip install pyautogui)

用python3 返回鼠標位置的實現(xiàn)方法(帶界面)

#!/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();
	#小數(shù)點個數(shù)
	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 返回鼠標位置的實現(xiàn)方法(帶界面)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI