Pergunta
Ubuntu 18.04.4 LTS
Python 3.6.9
<module 'PySimpleGUI' from '$HOME/temp/test/lib/python3.6/site-packages/PySimpleGUI/__init__.py'>
4.24.0 Released 3-Jul-2020
Experiência em programação Python: 4 anos.
Experiência geral em programação: 7 anos.
Já usou outro Python GUI Framework (tkinter, Qt, etc) anteriormente (sim / não está bem)? não.
É possível usar um ícone personalizado para FileBrowse ou FolderBrowse? Eu vi que esses dois atalhos são apenas invólucros para Button, pensei que poderia usar image_data, mas não funcionou.
Acho que posso imitar a funcionalidade com um botão, certo?
Amostra de código
#!/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()
Obviamente, recebo um erro:
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: Enquanto isso, estou criando o botão FileBrowser de antemão e, em seguida, modificando a variável ImageData.
Obrigado, ferramenta muito útil.
Suponho que você queira usar uma imagem no botão? Não é um ícone nesse caso, é uma imagem de botão
Como solução alternativa, você pode criar seu próprio botão de navegação de arquivo com uma imagem, copiando o código para FileBrowse e adicionando a imagem (nome de arquivo ou dados) à chamada de botão.
Aqui está o código FileBrowse
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)
Sim, quero dizer uma imagem. Vou usar isso, muito obrigado.
A única coisa que você realmente precisa adicionar à sua chamada de botão é o button_type. Isso é o que controlará se ele fizer uma pesquisa de arquivo, etc.
E talvez alguns dos parâmetros que possuem valores padrão (tipos de arquivo, por exemplo)