Skip to content
Advertisement

I was using pysimplegui and i want to position button on the right side

if i put element_justification as center i get button on the center, if i put element_justification on the right then text is also on the right even thoguht i putted justification in the text separately, i need text in center and button align to the right of the window.

    import PySimpleGUI as sg
    layout = [[sg.Text('Nešto ne štima', text_color="", font=(
        'Helvetica', 30), justification='center', key='rezultat1')],
        [sg.Text('Nije se spojilo na net', text_color="", font=(
            'Helvetica', 20), justification='center', visible=False, key='rezultat')],
        [sg.Button('?', size=(0, 0), visible=True, font=(
        'Helvetica', 20), key='go')], [sg.Button('Enter','center', visible=False, 
    key='gumb')]]
    win = sg.Window('Proba', layout, element_justification='center')
    while True:
        e, v = win.read()
        if e == 'go':
            win.Element('rezultat').Update('Nije se spojilo na net', visible=True)
        if e == sg.WIN_CLOSED:
            win.close()
            break

Advertisement

Answer

There’re something about the alignment of elements.

  1. Option justification in sg.Text means the alignment of multiline text in available space. So it will be nothing different if only one line and same space for it.

  2. element_justification in sg.Window means all elements in the Window itself will have this justification if not specified.

  3. To align an element, you need more space for element to aligned, otherwise there will be nothing different. So add a sg.Column as container for elements to align inside of it, and set expand_x to True to expand the space of sg.Column, after it, you can set element_justification of sg.Column to align the elements in it.

import PySimpleGUI as sg

col_layout = [
    [sg.Button('?', size=(0, 0), visible=True, font=('Helvetica', 20), key='go')],
    [sg.Button('Enter', visible=False, key='gumb')],    # second position argument for button type, cannot use 'center'
]

layout = [
    [sg.Text('Nešto ne štima', text_color="", font=('Helvetica', 30), key='rezultat1')],
    [sg.Text('Nije se spojilo na net', text_color="", font=('Helvetica', 20), visible=False, key='rezultat')],
    [sg.Column(col_layout, element_justification='right', expand_x=True)],
]
win = sg.Window('Proba', layout, element_justification='center')

while True:
    e, v = win.read()
    if e == sg.WIN_CLOSED:
        break
    elif e == 'go':
        win['rezultat'].update(visible=True)

win.close()
  1. Use sg.Push()/sg.VPush() to push other elements at same/different row in horizontal/vertical direction.
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement