我正在使用 pysimplegui 并且我想将按钮放在右侧

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

如果我把 element_justification 作为中心,我会在中心得到按钮,如果我把 element_justification 放在右边,那么即使我在文本中单独放置了理由,文本也会在右边,我需要居中的文本和按钮对齐 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

元素的对齐方式有些问题。

  1. sg.Text中的选项justification表示可用space中多行文本的对齐方式。所以如果只有一行和相同的 space 就没什么不同了。

  2. element_justification in sg.Window 表示如果未指定,Window 本身中的所有元素都将具有此理由。

  3. 要对齐一个元素,你需要更多space来对齐元素,否则没有什么不同。因此,添加一个 sg.Column 作为元素的容器以在其内部对齐,并将 expand_x 设置为 True 以扩展 sg.Column 的 space,在它之后,您可以设置 element_justification of sg.Column 来对齐其中的元素。

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. 使用 sg.Push()/sg.VPush() 将 same/different 行的其他元素推向 horizontal/vertical 方向。