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()
它将产生以下 输出 窗口 –
使用图形元素
你也可以用图形容器元素的 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()
它将产生以下 输出 窗口 –