【问题标题】:I was using pysimplegui and i want to position button on the right side我正在使用pysimplegui,我想将按钮放在右侧
【发布时间】:2022-02-22 20:38:10
【问题描述】:

如果我将 element_justification 作为中心,我会在中心获得按钮,如果我将 element_justification 放在右侧,那么文本也在右侧,即使我在文本中单独放置了对齐,我需要将文本居中并且按钮对齐到窗口右侧。

    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

【问题讨论】:

  • 我刚刚找到了答案:stackoverflow.com/questions/68929799/…
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python pysimplegui


【解决方案1】:

关于元素的对齐方式。

  1. sg.Text 中的选项justification 表示可用空间中多行文本的对齐方式。因此,如果只有一行和相同的空间,那将没有什么不同。

  2. sg.Window 中的element_justification 表示如果未指定,Window 本身中的所有元素都将具有此对齐方式。

  3. 要对齐一个元素,你需要更多的空间来对齐元素,否则不会有什么不同。所以添加一个sg.Column作为元素的容器在里面对齐,并设置expand_xTrue来扩展sg.Column的空间,在它之后,你可以设置sg.Columnelement_justification来对齐里面的元素。

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()在水平/垂直方向推动相同/不同行的其他元素。

【讨论】:

  • 非常感谢,你还帮我解决了我的最后一个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多