Avi*_*abu 4 python user-interface pysimplegui
在我的应用程序中,我试图将我的按钮、文本和输入放在窗口的中心。我使用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()
Run Code Online (Sandbox Code Playgroud)
我想这就是你要找的
window = sg.Window('My new window', layout, element_justification='c')
Run Code Online (Sandbox Code Playgroud)
编辑 - 垂直居中
在窗口中垂直居中布局是一件大事。需要的是 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()
Run Code Online (Sandbox Code Playgroud)
小智 3
此代码使文本、按钮和输入位于窗口的中心。
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()
Run Code Online (Sandbox Code Playgroud)