- PySimpleGUI 教程
- PySimpleGUI - 主页
- PySimpleGUI - 简介
- PySimpleGUI - 环境设置
- PySimpleGUI - Hello World
- PySimpleGUI - 弹出窗口
- PySimpleGUI - 窗口类
- PySimpleGUI - 元素类
- PySimpleGUI - 事件
- PySimpleGUI - 菜单栏
- PySimpleGUI - Matplotlib 集成
- PySimpleGUI - 使用 PIL
- PySimpleGUI - 调试器
- PySimpleGUI - 设置
- PySimpleGUI 实用资源
- PySimpleGUI - 简明指南
- PySimpleGUI - 实用资源
- PySimpleGUI - 讨论
PySimpleGUI - 图像元素
PySimpleGUI 库包含一个 Image 元素,它能够显示 PNG、GIF、PPM/PGM 格式的图像。Image() 函数需要一个必需参数,即图像文件的路径。
以下代码在应用程序窗口上显示 PySimpleGUI 徽标。
import PySimpleGUI as psg
layout = [[psg.Text(text='Python GUIs for Humans',
font=('Arial Bold', 16),
size=20, expand_x=True,
justification='center')],
[psg.Image('PySimpleGUI_Logo.png',
expand_x=True, expand_y=True )]
]
window = psg.Window('HelloWorld', layout, size=(715,350), keep_on_top=True)
while True:
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
window.close()
它将生成以下输出窗口 −
使用 Graph 元素
你还可以使用 Graph 容器元素上的 draw_image() 方法在该元素上显示图像,如下所示 −
import PySimpleGUI as psg
graph = psg.Graph(canvas_size=(700, 300),
graph_bottom_left=(0, 0),
graph_top_right=(700, 300),
background_color='red',
enable_events=True,
drag_submits=True, key='graph')
layout = [
[graph],
[psg.Button('LEFT'), psg.Button('RIGHT'),
psg.Button('UP'), psg.Button('DOWN')]
]
window = psg.Window('Graph test', layout, finalize=True)
x1, y1 = 350, 150
id = graph.draw_image(filename="PySimpleGUI_Logo.png", location=(0, 300))
while True:
event, values = window.read()
if event == psg.WIN_CLOSED:
break
if event == 'RIGHT':
graph.MoveFigure(id, 10, 0)
if event == 'LEFT':
graph.MoveFigure(id, -10, 0)
if event == 'UP':
graph.MoveFigure(id, 0, 10)
if event == 'DOWN':
graph.MoveFigure(id, 0, -10)
if event == "graph+UP":
x2, y2 = values['graph']
graph.MoveFigure(id, x2 - x1, y2 - y1)
x1, y1 = x2, y2
window.close()
它将生成以下输出窗口 −
pysimplegui_element_class.htm
广告