Ai
4 Star 4 Fork 0

Gitee 极速下载/pysimplegui

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/PySimpleGUI/PySimpleGUI
克隆/下载
Demo_PNG_Viewer.py 4.03 KB
一键复制 编辑 原始数据 按行查看 历史
timkay@not.com 提交于 2024-02-12 23:14 +08:00 . First release of PySimpleGUI 5!
#!/usr/bin/env python
import PySimpleGUI as sg
import os
'''
Simple Image Browser
This is an early demo program, so perhaps not quite as sophisticated as later ones.
Copyright 2021-2023 PySimpleSoft, Inc. and/or its licensors. All rights reserved.
Redistribution, modification, or any other use of PySimpleGUI or any portion thereof is subject to the terms of the PySimpleGUI License Agreement available at https://eula.pysimplegui.com.
You may not redistribute, modify or otherwise use PySimpleGUI or its contents except pursuant to the PySimpleGUI License Agreement.
'''
def main():
# Get the folder containing the images from the user
folder = sg.popup_get_folder('Image folder to open')
if folder is None:
sg.popup_cancel('Cancelling')
return
# get list of PNG files in folder
png_files = [os.path.join(folder, f) for f in os.listdir(folder) if f.lower().endswith('.png')]
filenames_only = [f for f in os.listdir(folder) if f.lower().endswith('.png')]
if len(png_files) == 0:
sg.popup('No PNG images in folder')
return
# define menu layout
menu = [['File', ['Open Folder', 'Exit']], ['Help', ['About', ]]]
# define layout, show and read the window
col = [[sg.Text(png_files[0], size=(80, 3), key='-FILENAME-')],
[sg.Image(filename=png_files[0], key='-IMAGE-', expand_x=True, expand_y=True)],
[sg.Button('Next', size=(8, 2)), sg.Button('Prev', size=(8, 2)),
sg.Text('File 1 of {}'.format(len(png_files)), size=(15, 1), key='-FILENUM-')]]
col_files = [[sg.Listbox(values=filenames_only, size=(60, 30), key='-LISTBOX-', enable_events=True)],
[sg.Text('Select a file. Use scrollwheel or arrow keys on keyboard to scroll through files one by one.')]]
layout = [[sg.Menu(menu)], [sg.Col(col_files), sg.Col(col, expand_x=True, expand_y=True)]]
window = sg.Window('Image Browser', layout, return_keyboard_events=True, use_default_focus=False)
# loop reading the user input and displaying image, filename
filenum, filename = 0, png_files[0]
while True:
event, values = window.read()
# --------------------- Button & Keyboard ---------------------
if event == sg.WIN_CLOSED:
break
elif event in ('Next', 'MouseWheel:Down', 'Down:40', 'Next:34') and filenum < len(png_files)-1:
filenum += 1
filename = os.path.join(folder, filenames_only[filenum])
window['-LISTBOX-'].update(set_to_index=filenum, scroll_to_index=filenum)
elif event in ('Prev', 'MouseWheel:Up', 'Up:38', 'Prior:33') and filenum > 0:
filenum -= 1
filename = os.path.join(folder, filenames_only[filenum])
window['-LISTBOX-'].update(set_to_index=filenum, scroll_to_index=filenum)
elif event == 'Exit':
break
elif event == '-LISTBOX-':
filename = os.path.join(folder, values['-LISTBOX-'][0])
filenum = png_files.index(filename)
# ----------------- Menu choices -----------------
if event == 'Open Folder':
newfolder = sg.popup_get_folder('New folder', no_window=True)
if newfolder is None:
continue
folder = newfolder
png_files = [os.path.join(folder, f) for f in os.listdir(folder) if f.lower().endswith('.png')]
filenames_only = [f for f in os.listdir(folder) if f.lower().endswith('.png')]
window['-LISTBOX-'].update(values=filenames_only)
window.refresh()
filenum = 0
elif event == 'About':
sg.popup('Demo PNG Viewer Program',
'Please give PySimpleGUI a try!')
# update window with new image
window['-IMAGE-'].update(filename=filename)
# update window with filename
window['-FILENAME-'].update(filename)
# update page display
window['-FILENUM-'].update('File {} of {}'.format(filenum + 1, len(png_files)))
window.close()
if __name__ == '__main__':
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/mirrors/pysimplegui.git
git@gitee.com:mirrors/pysimplegui.git
mirrors
pysimplegui
pysimplegui
master

搜索帮助