Skip to content
Advertisement

Align image center pysimpleGui

I use image (and button) as so:

myImg = sg.Image(filename='off.png', key='_CAMIMAGE_')

layout1 = [[myImg],
          [sg.Button('Exit')]]

sg.Window(title="Lights", layout=layout1, size=(500,300), margins=(0, 0)).read()

How do I align the myImg to center? I tried google can’t find anything on how to do it. I read somebody suggested justification=’center’ but I don’t know where to put that. I tried [myImg, justification=’center’] didn’t work just crash the app.

Advertisement

Answer

Based on documentation you can use Column with justification='center' to center widgets.

It needs list of rows – like [[my_img]]

But it centers column in window, not widgets in column
so for row with Button it can need separated Column.

import PySimpleGUI as sg

my_img = sg.Image(filename='rgb.png', key='_CAMIMAGE_')

# doesn't center Button in Columns
#layout = [
#    [sg.Column([[my_img], [sg.Button('Exit')]], justification='center')],
#]

layout = [
    [sg.Column([[my_img]], justification='center')],
    [sg.Column([[sg.Button('Exit')]], justification='center')],
]

window = sg.Window(title="Lights", layout=layout, size=(500, 500), margins=(0, 0))
window.read()
window.close()

enter image description here


BTW:

justification='center' can be used also in Text to center text.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement