'pysimplegui put the images in a certain location
is it possible to change the position of an image or a button? I would need to set them an x and y of my choice and not just centered right or left
I would like to put an image in a certain position
for example in this simple program how do I put the image in a certain point?
import PySimpleGUI as sg
GREEN = 'carrozina.png'
ORANGE = 'cerchio-rosso.png'
RED = 'cerchio-verde.png'
layout = [
[sg.Image(GREEN, key='_IMAGE_')],
[sg.Image(GREEN)],
[sg.Button('Green'), sg.Button('Red'), sg.Button('Orange')]
]
window = sg.Window('My new window', layout)
while True: # Event Loop
event, values = window.Read()
if event is None:
break
if event == 'Green':
file = GREEN
elif event == 'Red':
file = RED
if event == 'Orange':
file = ORANGE
window.Element('_IMAGE_').Update(file)
window.Close()
Solution 1:[1]
Here's the code to demo how to draw and move figure on sg.Graph.
You can use left/right/up/down key to move the figure, but without auto repeat function.
import PySimpleGUI as sg
width, height = size = (640, 480)
step = 20
steps = {'Left:37':(-step, 0), 'Right:39':(+step, 0), 'Up:38':(0, +step), 'Down:40':(0, -step)}
w, h = 56, 56
data = sg.EMOJI_BASE64_HAPPY_LAUGH
layout = [[sg.Graph(size, (0, 0), size, background_color='green', key='Graph')]]
window = sg.Window('My new window', layout, finalize=True, return_keyboard_events=True)
graph = window['Graph']
x , y = (width-w)//2, (height+h)//2
figure = graph.draw_image(data=data, location=(x, y))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event in ('Left:37', 'Right:39', 'Up:38', 'Down:40'):
dx, dy = steps[event]
x, y = max(0, min(width-w, x+dx)), max(h, min(height, y+dy))
graph.relocate_figure(figure, x, y)
window.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jason Yang |
