예제1) ms그림판으로 아래와 같은 그림을 그리기

조건)

- ms그림판 실행파일명 : mspaint

 

 

import pyautogui as pg

pg.sleep(0.5)
pg.hotkey('win','r')
pg.write('mspaint')
pg.press('enter')

pg.sleep(3)
pg.getActiveWindow().maximize()

pg.sleep(1)
pg.moveTo(100,300)

distance = 500
space = 10

while distance>0:
    pg.drag(distance,0, duration=0.2)
    pg.drag(0,distance, duration=0.2)
    distance -= space
    pg.drag(-distance, 0, duration=0.2)
    pg.drag(0,-distance, duration=0.2)
    distance -= space

 

 

 

예제2) pyautogui 모듈과 random 모듈을 이용하여 Windows 그림판에 랜덤으로 7개의 사각형을 그리기

조건)

- 사용패키지 : pyautogui, random

코드)

import pyautogui as pg
import random

pg.sleep(0.5)
pg.hotkey('win','r')

pg.sleep(0.5)
pg.write('mspaint')
pg.press('enter')

pg.sleep(3)
pg.getActiveWindow().maximize()

pg.sleep(1)
location=pg.locateOnScreen('images\\rec.png', confidence=0.5) #사각형을 그리는 아이콘을 캡쳐해서 저장해놓을것.

pg.moveTo(location.left+10,location.top+10)
pg.click()

center_x=326
center_y=526
pg.moveTo(center_x,center_y)

distance = 100  # Initialize the distance
i = 0  # Initialize the loop counter
num_rand=[]

while i < 7:
    pg.mouseDown()
    pg.drag(distance, distance, duration=0.3)  # Drag the mouse
    pg.mouseUp()
    pg.move(100,100)
    pg.click()
    x = random.randint(70, 600)
    y = random.randint(260, 800)
    pg.moveTo(x, y)
     
    d = random.randint(-100, 100)  # Generate a random distance
    distance += d  # Add the random distance to the total distance
    
    i += 1  # Increment the loop counter
    num_rand.append(d)  #데이터프레임에서는 size가 자동 할당되었는데 그 외에는 apped를 써야하는 듯.
 
print(num_rand) #생성된 난수를 확인

 

 

+ Recent posts