PySimpleGui在弹出窗口中显示图像

2024-10-02 18:14:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在弹出窗口中显示图像。有人知道怎么做吗

我尝试了image=(path),但它不起作用

sg.popup_no_buttons('Text', title='Über uns', text_color=('#F7F6F2'), keep_on_top=True)

目前:

enter image description here

目标:

enter image description here


Tags: pathnotext图像imagetitlesgcolor
1条回答
网友
1楼 · 发布于 2024-10-02 18:14:58

确认tkinter的版本不是8.5,特别是对于MacOS用户,tkinter 8.5不支持PNG图像

使用sg.popup_no_buttons中的image选项,您可以将图像添加到弹出窗口中

选项image的值

  • str用于图像文件名(GIF或PNG),或
  • bytes用于图像的rawBase64
import PySimpleGUI as sg

sg.popup_no_buttons('Text', title='Über uns', text_color=('#F7F6F2'), keep_on_top=True, image=sg.EMOJI_BASE64_HAPPY_IDEA)
sg.popup_no_buttons('Text', title='Über uns', text_color=('#F7F6F2'), keep_on_top=True, image="D:/1.png")

图像未与sg.popup_no_buttons的中心对齐,您可以自己创建一个弹出窗口。演示代码如下:

from textwrap import wrap
import PySimpleGUI as sg

def popup(title, filename, message, width=70):

    lines = list(map(lambda line:wrap(line, width=width), message.split('\n')))
    height = sum(map(len, lines))
    message = '\n'.join(map('\n'.join, lines))

    layout = [
        [sg.Image(filename=filename, expand_x=True)],
        [sg.Text(message, size=(width, height), justification='center', expand_x=True)]
    ]

    sg.Window(title, layout, keep_on_top=True, modal=True).read(close=2000)

popup('Über uns', 'D:/emoji.png', 'message')

message = """
Python is an easy to learn, powerful programming language.
It has efficient high-level data structures and a simple but effective approach to object-oriented programming.
Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.
""".strip()

popup('Über uns', 'D:/emoji.png', message)

相关问题 更多 >