Pysimplegui: [问题] FileBrowse/FolderBrowse 使用自定义图片?

创建于 2020-07-16  ·  5评论  ·  资料来源: PySimpleGUI/PySimpleGUI

问题类型(增强、错误、错误、问题)

操作系统

Ubuntu 18.04.4 LTS

蟒蛇版

蟒蛇 3.6.9

PySimpleGUI 端口和版本

<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 等)(是/否很好)? 不。

您已完成以下步骤:

  • [x] 阅读有关如何提交问题的说明
  • [x] 通过主要文档http://www.PySimpleGUI.org搜索您的问题
  • [x] 如果不是 PySimpleGUI(Qt、WX、Remi),请通过自述文件搜索您的特定端口
  • [x] 寻找与您的目标类似的演示程序http://www.PySimpleGUI.com
  • [x] 注意GitHub上每个端口下也有Demo Programs
  • [x] 在调试器之外运行你的程序(从命令行)
  • [x] 搜索问题(打开和关闭)以查看是否已报告
  • [x] 再次尝试升级您的 PySimpleGUI.py 文件以使用 GitHub 上的当前文件。 您的问题可能已经解决,但尚未出现在 PyPI 上。

问题描述

是否可以为 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 变量。

谢谢,非常有用的工具。

enhancement

所有5条评论

我假设您的意思是在按钮上使用图像? 在那种情况下它不是图标,而是按钮图像

作为一种变通方法,您可以通过复制 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。 这将控制它是否进行文件浏览等。

也许有几个具有默认值的参数(例如文件类型)

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

ihouses picture ihouses  ·  6评论

ncotrb picture ncotrb  ·  4评论

eyeonus picture eyeonus  ·  6评论

lucasea777 picture lucasea777  ·  3评论

MikeTheWatchGuy picture MikeTheWatchGuy  ·  3评论