简体   繁体   中英

PySimpleGUI get selected extension from FileSaveAs dialog box

I created a FileSaveAs button in my PySimpleGUI application, and defined the available file_types to be 'png' and 'jpg', but I have no way of knowing which of these two options was selected by the user. In other words, unless explicitly entered by the user, the value I get does not include the file extension.

Here's the code:

import PySimpleGUI as sg

layout = [[
    sg.InputText(visible=False, enable_events=True, key='fig_path'),
    sg.FileSaveAs(
        key='fig_save',
        file_types=(('PNG', '.png'), ('JPG', '.jpg')),  # TODO: better names
    )
]]
window = sg.Window('Demo Application', layout, finalize=True)

fig_canvas_agg = None
while True:  # Event Loop
    event, values = window.Read()
    if (event == 'fig_path') and (values['fig_path'] != ''):
        print('Saving to:', values['fig_path'])
    if event is None:
        break

Example:

在此处输入图像描述

In the above case, the value will be "[some path]\Test\hello", instead of ending with "hello.png".

Any way of either getting the returned path to include the extension, or getting the extension value separately?

Add defaultextension="*.*" to tk.filedialog.asksaveasfilename ()

It's around line 3332 in ver 4.30.0 of pysimplegui.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM