[PYTHON] PySimpleGUI

reference

PySimpleGUI

0. Virtual environment preparation (venv)

I didn't want to pollute the global environment, so I decided to create a virtual environment with venv and work there.

Virtual environment startup


$ python -m venv myvenv
$ cd myvenv/
$ source bin/activate
(myvenv)$ pip list
pip (7.1.2)
setuptools (18.2)

1. Get started right away

1-1. Install PySimple GUI with pip

Installation


(myvenv)$ pip install pysimplegui
(myvenv)$ pip list
Package     Version
----------- -------
pip         20.2.2
PySimpleGUI 4.29.0
setuptools  18.2

1-2. Sample code

This Code

sample.py


import PySimpleGUI as sg

sg.theme('DarkAmber')   #Theme color
#Layout in the window
layout = [  [sg.Text('Some text on Row 1')],
            [sg.Text('Enter something on Row 2'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Cancel')] ]

#Create a window
window = sg.Window('Window Title', layout)
# "events"Of event loops and inputs to handle"Process to get values
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
        break
    print('You entered ', values[0])

window.close()

1-3. Execute

Run


(myvenv)$ python sample.py 

Launched window

スクリーンショット 2020-08-26 17.08.47.png

Character input

Type "Hello, World!" And press "Ok" ... スクリーンショット 2020-08-26 17.13.07.png

The characters entered in the terminal are output.

Terminal output


You entered   Hello, World!

Summary

Unlike web applications that use a browser, I felt that it was good because I could code with an awareness of "event loops".

Recommended Posts

PySimpleGUI