728x90
반응형

파이썬을 이용하여 마우스를 특정한 위치에 자동으로 클릭하는 프로그램을 만들어 보겠습니다. 특정 위치에 대한 정보를 얻고 그 위치에 마우스 포인터를 이동 시키고 몇초 간격으로 클릭을 실행하는 프로그램입니다. 프로그램은 GUI를 이용하여 만들어 볼 건데, 저도 처음 사용하는 라이브러리인 tkinter 를 사용해 보겠습니다. 간단한 설명 문서는 아래 사이트를 참조 하면 됩니다.

https://docs.python.org/ko/3/library/tkinter.html
 

tkinter — Tcl/Tk 파이썬 인터페이스 — Python 3.10.2 문서

tkinter — Tcl/Tk 파이썬 인터페이스 소스 코드: Lib/tkinter/__init__.py The tkinter package (《Tk interface》) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, including mac

docs.python.org

 

마우스를 제어하기 위하여 키보드나 마우스를 제어할 수 있는 pyput 과 pyautogui 을 이용합니다. 관련 사이트는 아래를 참조하면 됩니다.

https://pypi.org/project/pynput/
 

pynput

Monitor and control user input devices

pypi.org

https://pyautogui.readthedocs.io/en/latest/
 

Welcome to PyAutoGUI’s documentation! — PyAutoGUI documentation

Welcome to PyAutoGUI’s documentation! PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications. The API is designed to be simple. PyAutoGUI works on Windows, macOS, and Linux, and runs on Python 2

pyautogui.readthedocs.io

 

위 이미지와 같이 tkinter를 사용하여 다이얼로그를 만들도록 하겠습니다. 우선 tkinter를 사용하기 위해서 import를 해줍니다. 그리고 아래와 같이 소스를 코딩하면 위와 같이 나타납니다.

from tkinter import *

root = Tk() # Tk클래스 객체 생성
root.title("마우스 자동 클릭") # 타이틀 이름 설정
root.geometry("550x180") # grid 형식으로 너비x높이 설정(픽셀단위)

root.mainloop() # Tk 클래스 객체의 mainloop 실행하여 다이얼로그 표시함함

빈 다이얼로그 창에 코드를 추가하여 버튼과 입력창을 만들어 보겠습니다. 

from tkinter import *

root = Tk() # Tk클래스 객체 생성
root.title("마우스 자동 클릭") # 타이틀 이름 설정
root.geometry("550x180") # grid 형식으로 너비x높이 설정(픽셀단위)

################
label1 = Label(root, text="X좌표")
label1.grid(row=1, column=1)

entry1 = Entry(root, width=10)
entry1.grid(row=1, column=2)

label2 = Label(root, text="Y좌표")
label2.grid(row=1, column=3)

entry2 = Entry(root, width=10)
entry2.grid(row=1, column=4)

button1 = Button(root, text="마우스위치")
button1.grid(row=1, column=5)

#################
label3 = Label(root, text="반복횟수")
label3.grid(row=2, column=1)

entry3 = Entry(root, width=10)
entry3.grid(row=2, column=2)
entry3.insert("end","120")

label4 = Label(root, text="인터벌(초)")
label4.grid(row=2, column=3)

entry4 = Entry(root, width=10)
entry4.grid(row=2, column=4)
entry4.insert("end","30")

##################
label5 = Label(root, text="* 120 회 * 30초 = 3600초(60분) 동안 동작")
label5.grid(row=3, column=2, columnspan=4)

##################
button2 = Button(root, text="클릭 시작")
button2.grid(row=4, column=3)
##################

root.mainloop() # Tk 클래스 객체의 mainloop 실행하여 다이얼로그 표시함함

위 소스로 실행되면 다이얼로그 창에 원하는 위치에 버튼과 입력창을 만들 수 있습니다.

이제 버튼이 클릭했을 때 동작하는 함수를 만들어 보겠습니다. 각 버튼에 아래와 같이 클릭시 실행하는 함수를 선언 할 수 있습니다.

button1 = Button(root, text="마우스위치", command=lambda: mousePointerPos())
# (중략)
button2 = Button(root, text="클릭 시작", command = lambda:startMacro())

위에서 실행될 함수를 작성하여 줍니다.

def mousePointerPos():
    with mouse.Listener(
        on_click=posClick
    ) as listener: listener.join()
    entry1.insert("end", x1)
    entry2.insert("end", y1)

def posClick(x, y, button, pressed):
    if pressed:
        global x1
        global y1
        x1 = x
        y1 = y

    if not pressed:
        return False

def startMacro():
    click_num = int(entry3.get())
    intervalSec = int(entry4.get())

    ##반복시작
    for a in range(0, click_num):
        time.sleep(intervalSec)
        pyautogui.click(x1, y1)

아래는 전체 코드입니다.

 

from tkinter import *
from pynput import mouse
import pyautogui
import time

root = Tk() # Tk클래스 객체 생성
root.title("마우스 자동 클릭") # 타이틀 이름 설정
root.geometry("550x180") # grid 형식으로 너비x높이 설정(픽셀단위)

################
label1 = Label(root, text="X좌표")
label1.grid(row=1, column=1)

entry1 = Entry(root, width=10)
entry1.grid(row=1, column=2)

label2 = Label(root, text="Y좌표")
label2.grid(row=1, column=3)

entry2 = Entry(root, width=10)
entry2.grid(row=1, column=4)

button1 = Button(root, text="마우스위치", command=lambda: mousePointerPos())
button1.grid(row=1, column=5)

#################
label3 = Label(root, text="반복횟수")
label3.grid(row=2, column=1)

entry3 = Entry(root, width=10)
entry3.grid(row=2, column=2)
entry3.insert("end","120")

label4 = Label(root, text="인터벌(초)")
label4.grid(row=2, column=3)

entry4 = Entry(root, width=10)
entry4.grid(row=2, column=4)
entry4.insert("end","30")

# checkbutton1 = Checkbutton(root, text="무한")
# checkbutton1.grid(row=2, column=5)
##################
label5 = Label(root, text="* 120 회 * 30초 = 3600초(60분) 동안 동작")
label5.grid(row=3, column=2, columnspan=4)

##################
button2 = Button(root, text="클릭 시작", command = lambda:startMacro())
button2.grid(row=4, column=3)
##################


def mousePointerPos():
    with mouse.Listener(
        on_click=posClick
    ) as listener: listener.join()
    entry1.insert("end", x1)
    entry2.insert("end", y1)

def posClick(x, y, button, pressed):
    if pressed:
        global x1
        global y1
        x1 = x
        y1 = y

    if not pressed:
        return False

def startMacro():
    click_num = int(entry3.get())
    intervalSec = int(entry4.get())

    ##반복시작
    for a in range(0, click_num):
        time.sleep(intervalSec)
        pyautogui.click(x1, y1)

root.mainloop() # Tk 클래스 객체의 mainloop 실행하여 다이얼로그 표시함함

 

지금 까지 간단하게 다이얼로그에 버튼, 입력창을 만들고 이것을 마우스 위치를 알아내여 마우스 클릭을 자동으록 실행하는 프로그램을 만들어 보았습니다. 

728x90
반응형

+ Recent posts