pysimplegui 설치 방법
다음 명령으로 pysimplegui 를 설치합니다. venv와 같은 가상환경에서도 설치가 가능합니다.
참고로 필자는 selenium이 설치된 경로에 pysimplegui를 설치하였습니다.
설치 명령:
$ python -m pip install pysimplegui
기본 예제 :
import PySimpleGUI as sg
layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]
# Create the window
window = sg.Window("Demo", layout)
# Create an event loop
while True:
event, values = window.read()
# End program if user closes window or
# presses the OK button
if event == "OK" or event == sg.WIN_CLOSED:
break
window.close()
참고 예제:
# img_viewer.py
2
3import PySimpleGUI as sg
4import os.path
5
6# First the window layout in 2 columns
7
8file_list_column = [
9 [
10 sg.Text("Image Folder"),
11 sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12 sg.FolderBrowse(),
13 ],
14 [
15 sg.Listbox(
16 values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17 )
18 ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23 [sg.Text("Choose an image from list on left:")],
24 [sg.Text(size=(40, 1), key="-TOUT-")],
25 [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30 [
31 sg.Column(file_list_column),
32 sg.VSeperator(),
33 sg.Column(image_viewer_column),
34 ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41 event, values = window.read()
42 if event == "Exit" or event == sg.WIN_CLOSED:
43 break
44 # Folder name was filled in, make a list of files in the folder
45 if event == "-FOLDER-":
46 folder = values["-FOLDER-"]
47 try:
48 # Get list of files in folder
49 file_list = os.listdir(folder)
50 except:
51 file_list = []
52
53 fnames = [
54 f
55 for f in file_list
56 if os.path.isfile(os.path.join(folder, f))
57 and f.lower().endswith((".png", ".gif"))
58 ]
59 window["-FILE LIST-"].update(fnames)
60 elif event == "-FILE LIST-": # A file was chosen from the listbox
61 try:
62 filename = os.path.join(
63 values["-FOLDER-"], values["-FILE LIST-"][0]
64 )
65 window["-TOUT-"].update(filename)
66 window["-IMAGE-"].update(filename=filename)
67
68 except:
69 pass
70
71window.close()
출력 화면:
이 블로그의 다른 포스팅 읽기
'Integration & Python' 카테고리의 다른 글
파이썬 (Python) 스토리 (0) | 2021.02.25 |
---|---|
Pillow 파이썬 이미지 처리 라이브러리 (0) | 2021.02.25 |
Python으로 MS WORD 문서 작성하는 방법 (0) | 2021.02.09 |
파이썬 셀레니움 이미지 크롤링 예제 (0) | 2021.02.08 |
Python을 이용한 네이버 홈쇼핑 키워드 크롤링 예제 (0) | 2021.01.26 |
댓글