python点击保存鼠标坐标并且定时点击

import time
from pymouse import PyMouse

import sys
import threading
import datetime
from tkinter import *

#获取当前电脑时间
def getCurrentTime():
    current_time = datetime.datetime.now()
    pass

LOG_LINE_NUM = 0

class MY_GUI():
    def __init__(self,init_window_name):
        self.init_window_name = init_window_name
        self.s_position = None
        self.click_count = 0
        self.currnet_click_count = 0
        self.thread1 = threading.Thread(target=self.autoclick)
    #设置窗口
    def set_init_window(self):
        self.init_window_name.title("获取鼠标位置自动点击_v1.0")           #窗口名
        #self.init_window_name.geometry('320x160+10+10')                         #290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
        self.init_window_name.geometry('480x280+10+10')
        #self.init_window_name["bg"] = "pink"                                    #窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
        #self.init_window_name.attributes("-alpha",0.9)                          #虚化,值越小虚化程度越高
        self.dingshilable = Label(self.init_window_name,text="定时点击")
        self.dingshilable.grid(row=0, column=0)

        self.hourEntry= Entry(self.init_window_name,width=10)
        self.hourEntry.grid(row=0, column=1)
        self.hourlable = Label(self.init_window_name,text="时")
        self.hourlable.grid(row=0, column=2)

        self.minuteEntry= Entry(self.init_window_name,width=10)
        self.minuteEntry.grid(row=0, column=3)
        self.minutelable = Label(self.init_window_name,text="分")
        self.minutelable.grid(row=0, column=4)

        self.secondsEntry= Entry(self.init_window_name,width=10)
        self.secondsEntry.grid(row=0, column=5)
        self.secondslable = Label(self.init_window_name,text="秒")
        self.secondslable.grid(row=0, column=6)
        
        self.dianjilable = Label(self.init_window_name,text="点击间隔")
        self.dianjilable.grid(row=1, column=0)

        self.clickIntervalEntry= Entry(self.init_window_name,width=10)#间隔时间
        self.clickIntervalEntry.grid(row=1, column=1)

        self.clickCountLable = Label(self.init_window_name,text="点击次数")
        self.clickCountLable.grid(row=1, column=2)

        self.clickCountEntry= Entry(self.init_window_name,width=10)#次数
        self.clickCountEntry.grid(row=1, column=3)

        # #按钮
        #self.auto_button = Button(self.init_window_name, text="自动截屏开始", bg="lightblue", width=10,command=self.str_trans_to_sum)  # 调用内部方法  加()为直接调用
        self.getpos_button = Button(self.init_window_name, text="获取鼠标位置", bg="lightblue", width=14,command=self.get_mouse_position)
        self.getpos_button.grid(row=2, column=0)
        self.getpos_button = Button(self.init_window_name, text="自动点击", bg="lightblue", width=14,command=self.get_position_thread)
        self.getpos_button.grid(row=2, column=1)
        self.labeltips = Label(self.init_window_name, text="(点击获取位置后请不要随意拖动)",bg="red")
        self.labeltips.grid(row=2, column=2, columnspan=10)

        self.log_data_Text = Text(self.init_window_name, width=66, height=15) #日志框
        self.log_data_Text.grid(row=3, column=0, columnspan=10)
        pass
    
    def autoclick(self):
        
        m = PyMouse()
        s_clickInterval = int(self.clickIntervalEntry.get())
        s_clickCount = int(self.clickCountEntry.get())
        if s_clickCount <= 0 :
            s_clickCount = 1
        while True:
            time.sleep(s_clickInterval)
            if self.s_position:
                if s_clickCount > 0:
                    self.write_log_to_Text("Auto click ok")
                    m.move(self.s_position[0],self.s_position[1])
                    m.click(self.s_position[0],self.s_position[1])
                    s_clickCount = s_clickCount -1
                    self.click_count = self.click_count -1
                else:
                    self.write_log_to_Text("Auto click Completed")
                    break
                pass
            if not self.thread1.is_alive:
                break
        pass
    #返回point鼠标位置
    def GetMousePosition(self):
        time.sleep(2.5)
        self.currnet_click_count = self.clickCountEntry.get()
        m = PyMouse()
        a = m.position()  #获取当前坐标的位置
        return a
        #隔2.5秒获取一次鼠标位置

    def get_position_thread(self):
        try:
            self.thread1.start()
        except KeyboardInterrupt:
            print("Ctrl-c press...")
            sys.exit(1)

    #得到鼠标位置
    def get_mouse_position(self):
        if self.click_count == 0:
            self.click_count = self.click_count +1
            position = self.GetMousePosition()
            self.s_position = position
            if position:
                s = "INFO:Get Position success: {pos}".format(pos=position)
                try:
                    #输出到界面
                    self.write_log_to_Text(s)
                except:
                    self.write_log_to_Text(1.0,"Get Position失败")
                pass

    #获取当前时间
    def get_current_time(self):
        current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        return current_time


    #日志动态打印
    def write_log_to_Text(self,logmsg):
        global LOG_LINE_NUM
        current_time = self.get_current_time()
        logmsg_in = str(current_time) +" " + str(logmsg) + "\n"      #换行
        if LOG_LINE_NUM <= 7:
            self.log_data_Text.insert(END, logmsg_in)
            LOG_LINE_NUM = LOG_LINE_NUM + 1
        else:
            self.log_data_Text.delete(1.0,2.0)
            self.log_data_Text.insert(END, logmsg_in)


def gui_start():
    init_window = Tk()              #实例化出一个父窗口
    ZMJ_PORTAL = MY_GUI(init_window)
    # 设置根窗口默认属性
    ZMJ_PORTAL.set_init_window()
    init_window.mainloop()          #父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示

if __name__ == '__main__':
    gui_start()


# 在命令行模式底下,切换到.py所在的目录,运行命令
# installer -F hello.py --noconsole
# 参数说明:-F可以只生成一个exe文件,没加这个参数会生成一大堆的文件,
# --noconsole这个参数可以使运行应用程序的时候不会出现命令行窗口,不然一运行.exe应用程序的时候就会自带生成一个命令行窗口。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Teleger

你的支持是我前进的方向

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值