FrontEnd

[PySimpleGUI] 기본 구조

집빈지노 2021. 8. 19. 09:49

* 기본적인 구조

 

PySimpleGUI는 네가지 파트로 이루어져 있다.

 

1. Import

첫번째 파트는 Import로 파트로 PySimpleGUI library를 임포트하는 파트이다.

import PySimpleGUI as sg

2. Layout

두번째 파트는 Layout으로 window에서 보여지는 부분의 단위요소들을 설정하는 파트이다. 여기서 layout의 요소들을 elements나 widget이라고 부른다. element에는 key라는 특성이 있는데, 이것은 CSS의 id, 식별자와 비슷하다. element를 가리킬 때 key를 이용한다.

  - Button element의 key는 button의 text.

layout = [
    [sg.Text("Plot test")],
    [sg.Canvas(key="-CANVAS-")],  # key 선언
    [sg.Button("Ok")],
]

 

3. Window

세번째 파트인 window에서는 먼저 창의 title text를 선언하고, 위의 layout를 불러오는 것이 기본이다.

    1. There is 'Title' text, and then 'layout' (above) that constructs the viewing window.
window = sg.Window(
    "Matplotlib Single Graph",
    layout,
    location=(0,0),
    finalize=True,
    element_justification="center",
    font="Helvetica 18",
)

4. Event Loop

마지막 파트인 event loop에서는 GUI 창이 떠있는 동안에 입력이나 조작을 처리하기 위한 무한 루프이다.

여기서 event는 보통 사용자가 element를 조작하거나 변화를 주었을 때 trigger 된다. values는 element들의 text 혹은 수치 값.

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    print('Button = ', event)
    # Process menu choices
    if event == 'About...':
        sg.popup('About this program', 'Version 1.0', 'PySimpleGUI rocks...')
    elif event == 'Open':
        filename = sg.popup_get_file('file to open', no_window=True)
        print(filename)