使用 pysimplegui 将按钮与 window 的中心对齐

Align Button To The center Of the window using pysimplegui

在我的应用程序中,我试图将我的按钮、文本和输入放在 window.I 的中心,我正在使用 PySimpleGUI 来设计 buttons.For 对齐我在 code.But 上使用 justification='center' 属性的中心仍然不适合 window.

的中心

我正在使用的代码是

import PySimpleGUI as sg
from tkinter import * 
sg.theme('DarkAmber')  
layout = [ 
        [sg.Text('Enter the value',justification='center')],
        [sg.Input(justification='center')],
        [sg.Button('Enter','center')]
     ]


  window = sg.Window('My new window', layout, size=(500,300), grab_anywhere=True)

 while True:
event, values = window.read()   # Read the event that happened and the values dictionary
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':     # If user closed window with X or if user clicked "Exit" button then exit
    break
if event == 'Button':
  print('You pressed the button')
window.close()

以上代码输出 我怎样才能使文本、按钮和输入到 window 的中心?

此代码使文本、按钮和输入位于 window 的中心。

import PySimpleGUI as sg
sg.theme('DarkAmber')  
layout = [ 
        [sg.Text('Enter the value',justification='center',size=(100,1))],
        [sg.Input(justification='center',size=(100,1))],
        [sg.Button('Enter','center',size=(100,1))]
     ]


window = sg.Window('My new window', layout, size=(500,300), grab_anywhere=True)

while True:
    event, values = window.read()   # Read the event that happened and the values dictionary
    print(event, values)
    if event == None or event == 'Exit':     # If user closed window with X or if user clicked "Exit" button then exit
        break
    if event == 'Button':
      print('You pressed the button')
    window.close()

我想这就是您要找的东西

window = sg.Window('My new window', layout, element_justification='c')

编辑 - 垂直居中(请参阅下面的 2022 年 3 月编辑以获得更好的解决方案)

在 window 中垂直居中布局是一件比较麻烦的事情。需要的是 window 扩展时将扩展的 2 个元素。这是一个演示程序,是 PySimpleGUI 上演示的一部分 GitHub

import PySimpleGUI as sg

"""
    Center a column in a window
    Solves a very specific kind of layout.
    If you want to have something centered in a Window, this is a good way to do it
    The "trick" here is:
        * the first row of the layout has a Text element that expands vertically
        * the row with the Column has a text element that expands horizontally
    
    This expanding Text element is what will cause the Column element to be centered

    Used with permission
"""

def main():
    column_to_be_centered = [  [sg.Text('My Window')],
                [sg.Input(key='-IN-')],
                [sg.Text(size=(12,1), key='-OUT-')],
                [sg.Button('Go'), sg.Button('Exit')]  ]

    layout = [[sg.Text(key='-EXPAND-', font='ANY 1', pad=(0, 0))],  # the thing that expands from top
              [sg.Text('', pad=(0,0),key='-EXPAND2-'),              # the thing that expands from left
               sg.Column(column_to_be_centered, vertical_alignment='center', justification='center',  k='-C-')]]

    window = sg.Window('Window Title', layout, resizable=True,finalize=True)
    window['-C-'].expand(True, True, True)
    window['-EXPAND-'].expand(True, True, True)
    window['-EXPAND2-'].expand(True, False, True)

    while True:             # Event Loop
        event, values = window.read()
        print(event, values)
        if event == sg.WIN_CLOSED or event == 'Exit':
            break
        if event == 'Go':
            window['-OUT-'].update(values['-IN-'])
    window.close()

if __name__ == '__main__':
    main()

编辑 2022 年 3 月 18 日

就像 PySimpleGUI 中的许多东西一样,元素对齐等操作也在不断发展。同样,Whosebug 的问题是这些答案通常不会更新。这就是为什么我建议始终使用 GitHub 问题来提出问题并查看 Cookbook、eCookbook 和演示程序以获取最新技术。

去年引入了两个新元素,使水平和垂直对齐变得微不足道。这些元素是 PushVPush。你会发现 recipe in the eCookbook.

这些元素“推动”周围的其他元素。 Push 水平移动它们,VPush 垂直移动它们。

如果你在 elements/rows 的两边都用力,那么 elements/rows 就会居中。

除了使用这些 PushVPush 元素外,这里的布局与上例相同。此解决方案更短且更容易理解。

import PySimpleGUI as sg

def main():
    column_to_be_centered = [  [sg.Text('My Window')],
                [sg.Input(key='-IN-')],
                [sg.Text(size=(12,1), key='-OUT-')],
                [sg.Button('Go'), sg.Button('Exit')]]

    layout = [[sg.VPush()],
              [sg.Push(), sg.Column(column_to_be_centered,element_justification='c'), sg.Push()],
              [sg.VPush()]]

    window = sg.Window('Window Title', layout, size=(500,300))

    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED or event == 'Exit':
            break

    window.close()


if __name__ == '__main__':
    main()

最好的一个应该是选项 element_justification='center' for Container sg.Column,也许不是所有元素都需要对齐 window 的中心。

import PySimpleGUI as sg

sg.theme('DarkAmber')

layout_column = [
        [sg.Text('Enter the value',justification='center')],
        [sg.Input(justification='center', key='-INPUT-')],
        [sg.Button('Enter')]
]

layout = [[sg.Column(layout_column, element_justification='center')]]

window = sg.Window('My new window', layout, grab_anywhere=True)

while True:
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Button':
        print('You pressed the button')
window.close()

如果您想更直接地控制元素的显示位置,可以在几乎所有元素(按钮、文本、in 等)中使用 pad 关键字。

有两种有效格式:

  • pad=(<int>, <int>) 其中 pad[0] 是左右填充 pad[1] 在元素上方和下方填充。
  • pad=((<int>, <int>), (<int>, <int>)) 第一个元组是 向左(第一个元素)和向右(第二个元素)填充和 第二个元组在上方(第一)和下方(第二)填充。

因此,要使用 pad 关键字使代码示例中的元素居中,您可以这样做:

import PySimpleGUI as sg
sg.theme('DarkAmber')
layout = [
        [sg.Text('Enter the value', pad=(190, 0))],
        [sg.Input(justification='center', pad=(75, 0))],
        [sg.Button('Enter', pad=(215, 0))]
     ]


window = sg.Window('My new window', layout,
                   size=(500, 300), grab_anywhere=True)

while True:
    # Read the event that happened and the values dictionary
    event, values = window.read()
    print(event, values)
    # If user closed window with X or if user clicked "Exit" button then exit
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Button':
        print('You pressed the button')
    window.close()

注意:以上代码与原始代码略有不同,以符合 PEP8 标准。

结果是:

我认为最简单的解决方案是在按钮左侧插入一个空白标签。

中心按钮使用空标签

bt: 字典={'size':(4,1)} [sg.Text('',size=(3,1)),sg.Button('Add',**bt),sg.Button('Clear',** bt)]]