본문 바로가기
Integration & Python

파이심플GUI(PySimpleGUI) 예

by 누워서 코딩 2021. 2. 23.

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()

 

 

참고 예제:

 

PySimpleGUI: The Simple Way to Create a GUI With Python – Real Python

In this step-by-step tutorial, you'll learn how to create a cross-platform graphical user interface (GUI) using Python and PySimpleGUI. A graphical user interface is an application that has buttons, windows, and lots of other elements that the user can use

realpython.com

# 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()

 

출력 화면:


이 블로그의 다른 포스팅 읽기

 

Python으로 MS WORD 문서 작성하는 방법

Python으로 MS WORD 문서 작성하는 방법 업무를 하다보면 거의 동일한 문서이지만 날짜나 숫자의 부분만 업데이트해야 하는 경우가 많습니다. 이럴 경우 번거롭게 문서를 열고 부분을 찾아다니며

freernd.tistory.com

댓글