题
Ubuntu 18.04.4 LTS
蟒蛇 3.6.9
<module 'PySimpleGUI' from '$HOME/temp/test/lib/python3.6/site-packages/PySimpleGUI/__init__.py'>
4.24.0 Released 3-Jul-2020
Python 编程经验:4 年。
总体编程经验:7 年。
以前使用过另一个 Python GUI 框架(tkinter、Qt 等)(是/否很好)? 不。
是否可以为 FileBrowse 或 FolderBrowse 使用自定义图标? 我已经看到这两个快捷方式只是 Button 的包装器,我以为我可以使用 image_data,但没有用。
我想我可以用一个按钮来模仿这个功能,对吗?
代码示例
#!/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()
显然,我收到一个错误:
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'
编辑:同时,我正在预先创建 FileBrowser 按钮,然后修改 ImageData 变量。
谢谢,非常有用的工具。
我假设您的意思是在按钮上使用图像? 在那种情况下它不是图标,而是按钮图像
作为一种变通方法,您可以通过复制 FileBrowse 的代码并将图像(文件名或数据)添加到 Button 调用来创建自己的带有图像的文件浏览按钮。
这是文件浏览代码
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)
是的,我的意思是图像。 我会用的,非常感谢。
您真正需要添加到按钮调用中的唯一内容是 button_type。 这将控制它是否进行文件浏览等。
也许有几个具有默认值的参数(例如文件类型)