用python3 返回鼠标位置的实现方法(带界面)

yipeiwu_com5年前Python基础

点击获取后,返回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 返回鼠标位置的实现方法(带界面)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python入门_浅谈逻辑判断与运算符

Python入门_浅谈逻辑判断与运算符

这是关于Python的第6篇文章,主要介绍下逻辑判断与运算符。 (一) 逻辑判断: 如果要实现一个复杂的功能程序,逻辑判断必不可少。逻辑判断的最基本标准:布尔类型。 布尔类型只有两个值:...

Python实现12306火车票抢票系统

Python实现12306火车票抢票系统

Python实现12306火车票抢票系统效果图如下所示: 具体代码如下所示: import urllib.request as request import http.coo...

pyqt5利用pyqtDesigner实现登录界面

本文实例为大家分享了pyqt5利用pyqtDesigner实现登录界面的具体代码,供大家参考,具体内容如下 为便于操作 界面和逻辑分离 逻辑类: import sys import...

Python反射和内置方法重写操作详解

本文实例讲述了Python反射和内置方法重写操作。分享给大家供大家参考,具体如下: isinstance和issubclass isinstance(obj,cls)检查是否obj是否是...

浅谈pytorch卷积核大小的设置对全连接神经元的影响

浅谈pytorch卷积核大小的设置对全连接神经元的影响

3*3卷积核与2*5卷积核对神经元大小的设置 #这里kerner_size = 2*5 class CONV_NET(torch.nn.Module): #CONV_NET类继承nn...