Pysimplegui: [ Question] Use custom image for FileBrowse/FolderBrowse?

Created on 16 Jul 2020  ·  5Comments  ·  Source: PySimpleGUI/PySimpleGUI

Type of Issues (Enhancement, Error, Bug, Question)

Question

Operating System

Ubuntu 18.04.4 LTS

Python version

Python 3.6.9

PySimpleGUI Port and Version

<module 'PySimpleGUI' from '$HOME/temp/test/lib/python3.6/site-packages/PySimpleGUI/__init__.py'>

4.24.0 Released 3-Jul-2020

Your Experience Levels In Months or Years

Python programming experience: 4 years.
Programming experience overall: 7 years.
Have used another Python GUI Framework (tkinter, Qt, etc) previously (yes/no is fine)? no.

You have completed these steps:

  • [x] Read instructions on how to file an Issue
  • [x] Searched through main docs http://www.PySimpleGUI.org for your problem
  • [x] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [x] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • [x] Note that there are also Demo Programs under each port on GitHub
  • [x] Run your program outside of your debugger (from a command line)
  • [x] Searched through Issues (open and closed) to see if already reported
  • [x] Try again by upgrading your PySimpleGUI.py file to use the current one on GitHub. Your problem may have already been fixed but is not yet on PyPI.

Description of question

Is it possible to use a custom icon for FileBrowse or FolderBrowse? I've seen those two shorcuts are just wrappers for Button, I thought I could use image_data, but didn't work.
I think I can imitate the functionality with a button, right?

Paste your code here


Code sample

#!/usr/bin/env python3
import PySimpleGUI as sg

sg.theme('LightBlue')   # Add a touch of color
# All the stuff inside your window.
filepng = "iVBORw0KGgoAAAANSUhEUgAAABMAAAAZCAYAAADTyxWqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAAuQAAALkB4qdB6AAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAADlSURBVDiN7dW9SkNBEIbhRxO9A0kh/oAgsQ2kVfEqLFLkMmy8DRvrgFjbiWCV5B4CQirtrEQhCbHIHAzHKOeslueDj2WW3Xd2d2AHjjDEFPMC/sAdtq3QsCAk7xF2lkFrcaIarvC4KlvoAi08o4F1POEM42xRlqn7C0hcbY5bnGMS8RgHgp6im0g+wy4esJ8Kgx46Fs+0h+t6AuQY90vxK7ZwUgb2HmMjnNdGGdglXrCZmz/EaRYUreZP6maMvxTgmypYBatg/6a6xW9ZQxtvCYx2jDPSu1PeA2ii76tBlPUk9jc/AameX+bEqOgkAAAAAElFTkSuQmCC"

layout = [
    [sg.Text('Log File')],
    [sg.InputText('output/msf.log', key='inputlog'), sg.FileBrowse(target='inputlog', image_data=filepng)],
    ...
]

# Create the Window
window = sg.Window('AutoAuditor', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'test':   # if user closes window or clicks cancel
        break
    print('You entered ', values[0])

window.close()

Obviously, I get an error:

Traceback (most recent call last):
  File "./gui.py", line 9, in <module>
    [sg.InputText('output/msf.log', key='inputlog'), sg.FileBrowse(target='inputlog', image_data=fa.icons['file'])],
TypeError: FileBrowse() got an unexpected keyword argument 'image_data'

Edit: Meanwhile, I'm creating the FileBrowser button beforehand, and then modifying the ImageData variable.

Thanks, very helpful tool.

enhancement

All 5 comments

I assume you mean to use an image on the button? It's not an icon in that case, it's a button image

As a work-around, you can create your own File browse button with an image by copying the code for FileBrowse and adding the image (either filename or data) to the Button call.

Here's the FileBrowse code

def FileBrowse(button_text='Browse', target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), initial_folder=None,
               tooltip=None, size=(None, None), auto_size_button=None, button_color=None, change_submits=False,
               enable_events=False, font=None, disabled=False,
               pad=None, key=None, k=None, metadata=None):
    """

    :param button_text: text in the button (Default value = 'Browse')
    :type button_text: (str)
    :param target: key or (row,col) target for the button (Default value = (ThisRow, -1))
    :param file_types: filter file types (Default value = (("ALL Files", "*.*")))
    :type file_types: Tuple[Tuple[str, str], ...]
    :param initial_folder:  starting path for folders and files
    :param tooltip: text, that will appear when mouse hovers over the element
    :type tooltip: (str)
    :param size:  (w,h) w=characters-wide, h=rows-high
    :type size: (int, int)
    :param auto_size_button:  True if button size is determined by button text
    :type auto_size_button: (bool)
    :param button_color: button color (foreground, background)
    :type button_color: Tuple[str, str] or str
    :param change_submits: If True, pressing Enter key submits window (Default = False)
    :type change_submits: (bool)
    :param enable_events: Turns on the element specific events.(Default = False)
    :type enable_events: (bool)
    :param font: specifies the font family, size, etc
    :type font: Union[str, Tuple[str, int]]
    :param disabled: set disable state for element (Default = False)
    :type disabled: (bool)
    :param pad: Amount of padding to put around element in pixels (left/right, top/bottom)
    :type pad: (int, int) or ((int, int),(int,int)) or (int,(int,int)) or  ((int, int),int)
    :param key: key for uniquely identify this element (for window.FindElement)
    :type key: Union[str, int, tuple, object]
    :param k: Same as the Key. You can use either k or key. Which ever is set will be used.
    :type k: Union[str, int, tuple, object]
    :return: returns a button
    :rtype: (Button)
    """
    return Button(button_text=button_text, button_type=BUTTON_TYPE_BROWSE_FILE, target=target, file_types=file_types,
                  initial_folder=initial_folder, tooltip=tooltip, size=size, auto_size_button=auto_size_button,
                  change_submits=change_submits, enable_events=enable_events, disabled=disabled,
                  button_color=button_color, font=font, pad=pad, key=key, k=k, metadata=metadata)

Yes, I mean an image. I'll use that, thank you very much.

The only thing you really need to add to your button call is the button_type. That's what will control if it does a file browse, etc.

And maybe a couple of the parameters that have default values (file types for example)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MikeTheWatchGuy picture MikeTheWatchGuy  ·  5Comments

MikeTheWatchGuy picture MikeTheWatchGuy  ·  6Comments

mozesa picture mozesa  ·  5Comments

ihouses picture ihouses  ·  6Comments

MikeTheWatchGuy picture MikeTheWatchGuy  ·  3Comments