컴퓨터/Python

python pywinauto 계산기

풍경소리^^ 2023. 11. 15. 13:45

https://www.youtube.com/watch?v=1n9CtboH46E

pywinauto_20231115.ipynb
0.05MB

"B:\python\jupyternotebook\pywinauto_20231115.ipynb"

from pywinauto import Application, keyboard, findwindows

# 현재 윈도우 화면에 있는 프로세스 목록 리스트를 반환한다. 
# 리스트의 각 요소는 element 객체로 프로세스 id, 핸들값, 이름 등의 정보를 보유한다.  
procs = findwindows.find_elements()

for proc in procs:
    print(f"{proc} / 프로세스 : {proc.process_id}")
app = Application(backend='uia') # 과거 win32, 최신 uia
# time.sleep(5)
app.start(r"C:\WINDOWS\system32\notepad.exe")
notepad = app['notepad'] # 변수에 노트패드 윈도우 어플리케이션 객체를 할당
notepad.print_control_identifiers() # 노트패드의 컨트롤 요소를 트리로 모두 출력
notepad = app['Dialog'] # 변수에 노트패드 윈도우 어플리케이션 객체를 할당
notepad.print_control_identifiers() # 노트패드의 컨트롤 요소를 트리로 모두 출력
notepad = app['제목 없음 - Windows 메모장Dialog'] # 변수에 노트패드 윈도우 어플리케이션 객체를 할당
notepad.print_control_identifiers() # 노트패드의 컨트롤 요소를 트리로 모두 출력
notepad = app['Dialog'] # 변수에 노트패드 윈도우 어플리케이션 객체를 할당
notepad.print_control_identifiers() # 노트패드의 컨트롤 요소를 트리로 모두 출력
# 최대화
notepad.child_window(title="최대화", control_type="Button").click()
# notepad.menu_select("파일(F)->새로 만들기(N)")
notepad.print_control_identifiers() # 노트패드의 컨트롤 요소를 트리로 모두 출력
notepad['Edit'].type_keys("안녕하세요 오토코더 입니다.{ENTER}")
# 메모 입력 (띄어쓰기 하려면 반드시 with_spaces=True
notepad['Edit'].type_keys('Hello World', with_spaces=True)
# pywinauto 글자를 적고 엔터하고 test 글자 적기
notepad['Edit'].type_keys('{ENTER}{TAB}pywinauto{ENTER}test')
notepad.type_keys("{ENTER}Hello pywinauto!\n\t It’s a sample text^A",
                        with_spaces=True, # 띄어쓰기 허용
                        with_newlines=True, # \n 개행 허용
                        pause=0.1, # 0.1초마다 타이핑
                        with_tabs=True # \t 탭 허용
                      )
# 파일 저장
notepad.menu_select("파일(F)->저장(S)")
keyboard.send_keys('temp파일{ENTER}')

# 종료
keyboard.send_keys('%{F4}') # Alt + F4
notepad.print_control_identifiers() # 노트패드의 컨트롤 요소를 트리로 모두 출력
from pywinauto.application import Application
app = Application(backend="uia").start("notepad.exe")
app.UntitledNotepad.type_keys("%FX")
from pywinauto.application import Application
app = Application(backend="uia").start("notepad.exe")
from pywinauto.application import Application
app = Application(backend="uia").start("notepad.exe")
notepad = app['Dialog'] # 변수에 노트패드 윈도우 어플리케이션 객체를 할당
notepad.print_control_identifiers() # 노트패드의 컨트롤 요소를 트리로 모두 출력
from pywinauto.application import Application
app = Application(backend="uia").start("calc.exe")
# notepad = app['계산기'] # 변수에 노트패드 윈도우 어플리케이션 객체를 할당
# notepad.print_control_identifiers() # 노트패드의 컨트롤 요소를 트리로 모두 출력
from pywinauto import Application, keyboard, findwindows
# import time

# 현재 윈도우 화면에 있는 프로세스 목록 리스트를 반환한다. 
# 리스트의 각 요소는 element 객체로 프로세스 id, 핸들값, 이름 등의 정보를 보유한다.  
procs = findwindows.find_elements()

for proc in procs:
    print(f"{proc} / 프로세스 : {proc.process_id}")

외부파일로 저장

from pywinauto import findwindows

procs = findwindows.find_elements()

with open('out.txt', 'w') as file:
    for proc in procs:
        file.write(f"{proc} / 프로세스 : {proc.process_id}\n")

print("결과가 out.txt 파일에 저장되었습니다.")

계산기#############################################

from pywinauto import Application, Desktop, findwindows, keyboard
import re

# 계산기 응용 프로그램 실행
app = Application(backend="uia").start('calc.exe')
# 현재 윈도우 화면에 있는 프로세스 목록 리스트를 반환한다. 
# 리스트의 각 요소는 element 객체로 프로세스 id, 핸들값, 이름 등의 정보를 보유한다.  
# procs = findwindows.find_elements()

# for proc in procs:
#     print(f"{proc} / 프로세스 : {proc.process_id}")

# 메인 창이 나타날 때까지 대기 (계산기 창의 타이틀을 기반으로 가정)
dlg = Desktop(backend="uia")['계산기0']
# dlg.type_keys('1{+}2=')
# dlg.type_keys('2{*}2{*}2=')
dlg.type_keys('9')
dlg.child_window(auto_id="squareRootButton").click()
dlg.child_window(auto_id="equalButton").click()
dlg.wait('visible')
# dlg.print_control_identifiers()

# 결과 값을 출력
result_text = dlg.child_window(auto_id="CalculatorResults")
result = result_text.texts()
# # print(result)

numbers = re.sub(r'[^0-9]', '', result[0])
print(numbers)

# 종료
keyboard.send_keys('%{F4}') # Alt + F4
dlg.print_control_identifiers()

Control Identifiers:

Dialog - '계산기'    (L1198, T3, R1534, B544)
['Dialog', '계산기Dialog', '계산기', 'Dialog0', 'Dialog1', '계산기Dialog0', '계산기Dialog1', '계산기0', '계산기1']
child_window(title="계산기", control_type="Window")
   |
   | Dialog - '계산기'    (L1338, T4, R1526, B36)
   | ['Dialog2', '계산기Dialog2', '계산기2']
   | child_window(title="계산기", auto_id="TitleBar", control_type="Window")
   |    |
   |    | Menu - '시스템'    (L0, T0, R0, B0)
   |    | ['시스템Menu', 'Menu', '시스템', '시스템0', '시스템1']
   |    | child_window(title="시스템", auto_id="SystemMenuBar", control_type="MenuBar")
   |    |    |
   |    |    | MenuItem - '시스템'    (L0, T0, R0, B0)
   |    |    | ['MenuItem', '시스템MenuItem', '시스템2']
   |    |    | child_window(title="시스템", control_type="MenuItem")
   |    |
   |    | Button - '계산기 최소화'    (L1388, T4, R1434, B36)
   |    | ['계산기 최소화', '계산기 최소화Button', 'Button', 'Button0', 'Button1']
   |    | child_window(title="계산기 최소화", auto_id="Minimize", control_type="Button")
   |    |
   |    | Button - '계산기 최대화'    (L1434, T4, R1480, B36)
   |    | ['계산기 최대화', '계산기 최대화Button', 'Button2']
   |    | child_window(title="계산기 최대화", auto_id="Maximize", control_type="Button")
   |    |
   |    | Button - '계산기 닫기'    (L1480, T4, R1526, B36)
   |    | ['계산기 닫기Button', 'Button3', '계산기 닫기']
   |    | child_window(title="계산기 닫기", auto_id="Close", control_type="Button")
   |
   | Dialog - '계산기'    (L1206, T4, R1526, B536)
   | ['Dialog3', '계산기Dialog3', '계산기3']
   | child_window(title="계산기", control_type="Window")
   |    |
   |    | Static - '계산기'    (L1254, T12, R1290, B28)
   |    | ['Static', '계산기Static', '계산기4', 'Static0', 'Static1']
   |    | child_window(title="계산기", auto_id="AppName", control_type="Text")
   |    | 
   |    | Custom - ''    (L1206, T36, R1526, B536)
   |    | ['계산기Custom', 'Custom']
   |    | child_window(auto_id="NavView", control_type="Custom")
   |    |    |
   |    |    | Button - '탐색 열기'    (L1210, T42, R1250, B78)
   |    |    | ['Button4', '탐색 열기', '탐색 열기Button']
   |    |    | child_window(title="탐색 열기", auto_id="TogglePaneButton", control_type="Button")
   |    |    |    |
   |    |    |    | Static - ''    (L0, T0, R0, B0)
   |    |    |    | ['Static2']
   |    |    |    | child_window(auto_id="PaneTitleTextBlock", control_type="Text")
   |    |    |
   |    |    | GroupBox - ''    (L1206, T44, R1526, B535)
   |    |    | ['계산기GroupBox', 'GroupBox', 'GroupBox0', 'GroupBox1']
   |    |    |    | 
   |    |    |    | Static - '식은 √(9)='    (L1224, T88, R1508, B107)
   |    |    |    | ['Static3', '식은 √(9)=Static', '식은 √(9)=']
   |    |    |    | child_window(title="식은 √(9)=", auto_id="CalculatorExpression", control_type="Text")
   |    |    |    |
   |    |    |    | Static - '표시는 3'    (L1206, T107, R1526, B182)
   |    |    |    | ['Static4', '표시는 3Static', '표시는 3']
   |    |    |    | child_window(title="표시는 3", auto_id="CalculatorResults", control_type="Text")
   |    |    |    |
   |    |    |    | Button - '검색 기록 플라이아웃 열기'    (L1486, T44, R1518, B76)
   |    |    |    | ['검색 기록 플라이아웃 열기Button', 'Button5', '검색 기록 플라이아웃 열기']
   |    |    |    | child_window(title="검색 기록 플라이아웃 열기", auto_id="HistoryButton", control_type="Button")
   |    |    |    |
   |    |    |    | GroupBox - '메모리 제어'    (L1210, T183, R1522, B214)
   |    |    |    | ['메모리 제어GroupBox', '메모리 제어', 'GroupBox2']
   |    |    |    | child_window(title="메모리 제어", auto_id="MemoryPanel", control_type="Group")
   |    |    |    |    | 
   |    |    |    |    | Button - '모든 메모리 지우기'    (L1210, T183, R1260, B214)
   |    |    |    |    | ['모든 메모리 지우기', 'Button6', '모든 메모리 지우기Button']
   |    |    |    |    | child_window(title="모든 메모리 지우기", auto_id="ClearMemoryButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '메모리 값 읽기'    (L1262, T183, R1312, B214)
   |    |    |    |    | ['Button7', '메모리 값 읽기Button', '메모리 값 읽기']
   |    |    |    |    | child_window(title="메모리 값 읽기", auto_id="MemRecall", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '메모리 값 더하기'    (L1314, T183, R1364, B214)
   |    |    |    |    | ['메모리 값 더하기', '메모리 값 더하기Button', 'Button8']
   |    |    |    |    | child_window(title="메모리 값 더하기", auto_id="MemPlus", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '메모리 값 빼기'    (L1366, T183, R1416, B214)
   |    |    |    |    | ['메모리 값 빼기Button', '메모리 값 빼기', 'Button9']
   |    |    |    |    | child_window(title="메모리 값 빼기", auto_id="MemMinus", control_type="Button")
   |    |    |    |    | 
   |    |    |    |    | Button - '메모리 값 저장'    (L1418, T183, R1469, B214)
   |    |    |    |    | ['메모리 값 저장', 'Button10', '메모리 값 저장Button']
   |    |    |    |    | child_window(title="메모리 값 저장", auto_id="memButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '메모리 플라이아웃 열기'    (L1472, T183, R1522, B214)
   |    |    |    |    | ['메모리 플라이아웃 열기Button', '메모리 플라이아웃 열기', 'Button11']
   |    |    |    |    | child_window(title="메모리 플라이아웃 열기", auto_id="MemoryButton", control_type="Button")
   |    |    |    |
   |    |    |    | GroupBox - '디스플레이 제어'    (L1210, T216, R1522, B267)
   |    |    |    | ['디스플레이 제어GroupBox', '디스플레이 제어', 'GroupBox3']
   |    |    |    | child_window(title="디스플레이 제어", auto_id="DisplayControls", control_type="Group")
   |    |    |    |    | 
   |    |    |    |    | Button - '백분율'    (L1210, T216, R1287, B267)
   |    |    |    |    | ['Button12', '백분율', '백분율Button']
   |    |    |    |    | child_window(title="백분율", auto_id="percentButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '입력 지우기'    (L1289, T216, R1365, B267)
   |    |    |    |    | ['Button13', '입력 지우기', '입력 지우기Button']
   |    |    |    |    | child_window(title="입력 지우기", auto_id="clearEntryButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '지우기'    (L1367, T216, R1444, B267)
   |    |    |    |    | ['지우기', '지우기Button', 'Button14']
   |    |    |    |    | child_window(title="지우기", auto_id="clearButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '백스페이스'    (L1446, T216, R1522, B267)
   |    |    |    |    | ['백스페이스', '백스페이스Button', 'Button15']
   |    |    |    |    | child_window(title="백스페이스", auto_id="backSpaceButton", control_type="Button")
   |    |    |    | 
   |    |    |    | GroupBox - '표준 함수'    (L1210, T269, R1444, B320)
   |    |    |    | ['표준 함수GroupBox', 'GroupBox4', '표준 함수']
   |    |    |    | child_window(title="표준 함수", auto_id="StandardFunctions", control_type="Group")
   |    |    |    |    |
   |    |    |    |    | Button - '역'    (L1210, T269, R1287, B320)
   |    |    |    |    | ['역Button', '역', 'Button16']
   |    |    |    |    | child_window(title="역", auto_id="invertButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '사각형'    (L1289, T269, R1365, B320)
   |    |    |    |    | ['사각형', '사각형Button', 'Button17']
   |    |    |    |    | child_window(title="사각형", auto_id="xpower2Button", control_type="Button")
   |    |    |    |    | 
   |    |    |    |    | Button - '제곱근'    (L1367, T269, R1444, B320)
   |    |    |    |    | ['제곱근', '제곱근Button', 'Button18']
   |    |    |    |    | child_window(title="제곱근", auto_id="squareRootButton", control_type="Button")
   |    |    |    |
   |    |    |    | GroupBox - '표준 연산자'    (L1446, T269, R1522, B532)
   |    |    |    | ['표준 연산자', '표준 연산자GroupBox', 'GroupBox5']
   |    |    |    | child_window(title="표준 연산자", auto_id="StandardOperators", control_type="Group")
   |    |    |    |    | 
   |    |    |    |    | Button - '나누기'    (L1446, T269, R1522, B320)
   |    |    |    |    | ['나누기', '나누기Button', 'Button19']
   |    |    |    |    | child_window(title="나누기", auto_id="divideButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '곱'    (L1446, T322, R1522, B373)
   |    |    |    |    | ['곱Button', '곱', 'Button20']
   |    |    |    |    | child_window(title="곱", auto_id="multiplyButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '음의 값'    (L1446, T375, R1522, B426)
   |    |    |    |    | ['음의 값', '음의 값Button', 'Button21']
   |    |    |    |    | child_window(title="음의 값", auto_id="minusButton", control_type="Button")
   |    |    |    |    | 
   |    |    |    |    | Button - '양의 값'    (L1446, T428, R1522, B479)
   |    |    |    |    | ['Button22', '양의 값', '양의 값Button']
   |    |    |    |    | child_window(title="양의 값", auto_id="plusButton", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '일치'    (L1446, T481, R1522, B532)
   |    |    |    |    | ['일치Button', 'Button23', '일치']
   |    |    |    |    | child_window(title="일치", auto_id="equalButton", control_type="Button")
   |    |    |    | 
   |    |    |    | GroupBox - '숫자 패드'    (L1210, T322, R1444, B532)
   |    |    |    | ['숫자 패드', '숫자 패드GroupBox', 'GroupBox6']
   |    |    |    | child_window(title="숫자 패드", auto_id="NumberPad", control_type="Group")
   |    |    |    |    |
   |    |    |    |    | Button - '공'    (L1289, T481, R1366, B532)
   |    |    |    |    | ['공', '공Button', 'Button24']
   |    |    |    |    | child_window(title="공", auto_id="num0Button", control_type="Button")
   |    |    |    |    | 
   |    |    |    |    | Button - '1'    (L1210, T428, R1287, B479)
   |    |    |    |    | ['1Button', '1', 'Button25']
   |    |    |    |    | child_window(title="1", auto_id="num1Button", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '2'    (L1289, T428, R1366, B479)
   |    |    |    |    | ['2Button', '2', 'Button26']
   |    |    |    |    | child_window(title="2", auto_id="num2Button", control_type="Button")
   |    |    |    |    | 
   |    |    |    |    | Button - '3'    (L1368, T428, R1444, B479)
   |    |    |    |    | ['Button27', '3Button', '3']
   |    |    |    |    | child_window(title="3", auto_id="num3Button", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '4'    (L1210, T375, R1287, B426)
   |    |    |    |    | ['4Button', '4', 'Button28']
   |    |    |    |    | child_window(title="4", auto_id="num4Button", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '5'    (L1289, T375, R1366, B426)
   |    |    |    |    | ['5', '5Button', 'Button29']
   |    |    |    |    | child_window(title="5", auto_id="num5Button", control_type="Button")
   |    |    |    |    | 
   |    |    |    |    | Button - '6'    (L1368, T375, R1444, B426)
   |    |    |    |    | ['6', 'Button30', '6Button']
   |    |    |    |    | child_window(title="6", auto_id="num6Button", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '7'    (L1210, T322, R1287, B373)
   |    |    |    |    | ['7', '7Button', 'Button31']
   |    |    |    |    | child_window(title="7", auto_id="num7Button", control_type="Button")
   |    |    |    |    | 
   |    |    |    |    | Button - '8'    (L1289, T322, R1366, B373)
   |    |    |    |    | ['8Button', '8', 'Button32']
   |    |    |    |    | child_window(title="8", auto_id="num8Button", control_type="Button")
   |    |    |    |    |
   |    |    |    |    | Button - '9'    (L1368, T322, R1444, B373)
   |    |    |    |    | ['9', '9Button', 'Button33']
   |    |    |    |    | child_window(title="9", auto_id="num9Button", control_type="Button")
   |    |    |    |    | 
   |    |    |    |    | Button - '소수점 구분 기호'    (L1368, T481, R1444, B532)
   |    |    |    |    | ['소수점 구분 기호', '소수점 구분 기호Button', 'Button34']
   |    |    |    |    | child_window(title="소수점 구분 기호", auto_id="decimalSeparatorButton", control_type="Button")
   |    |    |    |
   |    |    |    | Button - '양수 음수'    (L1210, T481, R1287, B532)
   |    |    |    | ['양수 음수', 'Button35', '양수 음수Button']
   |    |    |    | child_window(title="양수 음수", auto_id="negateButton", control_type="Button")
   |    |    | 
   |    |    | Static - '표준'    (L1254, T45, R1294, B72)
   |    |    | ['Static5', '표준', '표준Static']
   |    |    | child_window(title="표준", auto_id="Header", control_type="Text")
   |    |    |
   |    |    | Button - '항상 위에 유지'    (L1304, T44, R1336, B76)
   |    |    | ['항상 위에 유지', '항상 위에 유지Button', 'Button36']
   |    |    | child_window(title="항상 위에 유지", auto_id="NormalAlwaysOnTopButton", control_type="Button")
   | 
   | Pane - ''    (L1206, T36, R1526, B536)
   | ['계산기Pane', 'Pane']
3