python 使用pysimplegui将按钮与窗口中心对齐

s4chpxco  于 2023-01-01  发布在  Python
关注(0)|答案(5)|浏览(644)

在我的应用程序中,我试图将按钮、文本和输入放在窗口的中心。我使用PySimpleGUI设计按钮。为了对齐中心,我在代码中使用了justification='center'属性。但它仍然不适合窗口的中心。
我使用的代码是

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()

上面的代码输出

我怎样才能使文本,按钮和输入到窗口的中心?

kgsdhlau

kgsdhlau1#

我想这就是你要找的

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

编辑-垂直居中(更好的解决方案见下文2022年3月编辑)
在窗口中垂直居中布局是一件相当麻烦的事情。需要的是两个元素,当窗口扩展时,它们会扩展。这是一个演示程序,是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中的许多东西一样,像元素调整这样的操作也在不断地发展。StackOverflow的问题是这些答案通常不会更新。这就是为什么我建议总是使用GitHub Issues来提问,并检查Cookbook、eCookbook和Demo Programs来获得最新的技术。
去年引入了两个新元素,使水平和垂直对齐变得微不足道。这两个元素是PushVPush。你可以在eCookbook中找到一个食谱。
这些元素“推动”周围的其他元素。Push水平移动它们,VPush垂直移动它们。
如果您在元素/行的两侧都按下按钮,则这些元素/行将居中。
除了使用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()

0g0grzrc

0g0grzrc2#

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

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()
yks3o0rb

yks3o0rb3#

最好的一个应该是容器sg.Column的选项element_justification='center',可能不是所有的元素都需要对齐窗口的中心。

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()
ilmyapht

ilmyapht4#

如果希望更直接地控制元素的显示位置,可以在几乎所有元素(button、text、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标准。
结果是:

gj3fmq9x

gj3fmq9x5#

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

# center buttons using an empty label
bt: dict={'size':(4,1)}
[sg.Text('',size=(3,1)), sg.Button('Add',**bt), sg.Button('Clear',**bt)]]

相关问题