对齐图像中心 pysimpleGui
Align image center pysimpleGui
我这样使用图像(和按钮):
myImg = sg.Image(filename='off.png', key='_CAMIMAGE_')
layout1 = [[myImg],
[sg.Button('Exit')]]
sg.Window(title="Lights", layout=layout1, size=(500,300), margins=(0, 0)).read()
如何将 myImg 居中对齐?我试过了 google 找不到任何东西
关于如何去做。我读到有人建议 justification='center' 但我不知道
放在哪里。我试过 [myImg, justification='center'] 没用只是让应用程序崩溃。
根据文档,您可以使用 Column 和 justification='center'
来居中小部件。
它需要行列表 - 如 [[my_img]]
但它在 window
中居中 column
,而不是 column
中的 widgets
所以对于 Button
的行,它可能需要分开 Column
.
import PySimpleGUI as sg
my_img = sg.Image(filename='rgb.png', key='_CAMIMAGE_')
# doesn't center Button in Columns
#layout = [
# [sg.Column([[my_img], [sg.Button('Exit')]], justification='center')],
#]
layout = [
[sg.Column([[my_img]], justification='center')],
[sg.Column([[sg.Button('Exit')]], justification='center')],
]
window = sg.Window(title="Lights", layout=layout, size=(500, 500), margins=(0, 0))
window.read()
window.close()
BTW:
justification='center'
也可用于 Text
以居中文本。
以下代码显示了它如何针对 sg.Column
的选项 justification
、element_justification
、vertical_alignment
、expand_x
和 expand_y
工作。
这里横向有两个sg.Column
,只有右边一个会被不同的选项调整
- 对于选项
justification
,它是针对sg.Column
本身的,当没有额外的space时,它不会显示任何不同。设置 expand_x=False
,将有额外的 space 和选项 justification
将起作用。实际上,它适用于这一整行,而不仅仅是 sg.Column
。本行仅最后sg.Column
,设置最终归属justification
.
- 对于选项
element_justification
,它适用于此 sg.Column
中的所有元素,如果您通过选项 expand_x=True
提供更多 space,它将起作用。
- 对于选项
vertical_alignment
,如果有更多 space
this sg.Column
在“这一行”的垂直方向上。设置expand_y=False
,所以这个sg.Column
的高度会低于这一行的最大高度,然后就可以了。
- 对于选项
expand_x
和 expand_y
,如果为 True,此列将自动向 X/Y 方向扩展以填充可用的 space.
import PySimpleGUI as sg
def make_window(window=None, justification='left', element_justification='left',
vertical_alignment='top', expand_x=True, expand_y=True):
column_layout = [
[sg.Text("(0, 0)", text_color='black', background_color='yellow'),
sg.Text("(0, 1)", text_color='black', background_color='green')],
[sg.Text("(1, 0)", text_color='black', background_color='red'),
sg.Text("(1, 1)", text_color='black', background_color='blue')],
]
layout = [
[
sg.Column([[sg.Text("Fixed Column")]], size=(200, 200), background_color='blue'),
sg.Column(
column_layout,
justification=justification,
element_justification=element_justification,
expand_x=expand_x,
expand_y=expand_y,
vertical_alignment=vertical_alignment,
background_color='gray',
key='Column'),
],
]
radios = []
for group, text in [
("justification", ("left", "center", "right")),
("element_justification", ("left", "center", "right")),
("vertical_alignment", ("top", "center", "bottom")),
("expand_x", (True, False)),
("expand_y", (True, False))]:
row = []
row.append(sg.Text(group, size=(25, 1)))
for i, value in enumerate(text):
row.append(sg.Radio(str(value), group, default=eval(f"{group}==value"), size=(6, 1), enable_events=True, key=(group, value)))
radios.append(row)
layout += radios
win = sg.Window(title="Lights", layout=layout, size=(800, 400), margins=(0, 0), finalize=True)
if window:
window.close()
return win
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 12))
window = make_window()
justification, element_justification, vertical_alignment, expand_x, expand_y = (
'left', 'left', 'top', True, True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif isinstance(event, tuple):
radio = event[0]
if radio == 'justification':
justification = event[1]
elif radio == 'element_justification':
element_justification = event[1]
elif radio == 'vertical_alignment':
vertical_alignment = event[1]
elif radio == 'expand_x':
expand_x = event[1]
elif radio == 'expand_y':
expand_y = event[1]
window = make_window(window, justification=justification,
element_justification=element_justification,
vertical_alignment=vertical_alignment, expand_x=expand_x,
expand_y=expand_y)
window.close()
我这样使用图像(和按钮):
myImg = sg.Image(filename='off.png', key='_CAMIMAGE_')
layout1 = [[myImg],
[sg.Button('Exit')]]
sg.Window(title="Lights", layout=layout1, size=(500,300), margins=(0, 0)).read()
如何将 myImg 居中对齐?我试过了 google 找不到任何东西 关于如何去做。我读到有人建议 justification='center' 但我不知道 放在哪里。我试过 [myImg, justification='center'] 没用只是让应用程序崩溃。
根据文档,您可以使用 Column 和 justification='center'
来居中小部件。
它需要行列表 - 如 [[my_img]]
但它在 window
中居中 column
,而不是 column
中的 widgets
所以对于 Button
的行,它可能需要分开 Column
.
import PySimpleGUI as sg
my_img = sg.Image(filename='rgb.png', key='_CAMIMAGE_')
# doesn't center Button in Columns
#layout = [
# [sg.Column([[my_img], [sg.Button('Exit')]], justification='center')],
#]
layout = [
[sg.Column([[my_img]], justification='center')],
[sg.Column([[sg.Button('Exit')]], justification='center')],
]
window = sg.Window(title="Lights", layout=layout, size=(500, 500), margins=(0, 0))
window.read()
window.close()
BTW:
justification='center'
也可用于 Text
以居中文本。
以下代码显示了它如何针对 sg.Column
的选项 justification
、element_justification
、vertical_alignment
、expand_x
和 expand_y
工作。
这里横向有两个sg.Column
,只有右边一个会被不同的选项调整
- 对于选项
justification
,它是针对sg.Column
本身的,当没有额外的space时,它不会显示任何不同。设置expand_x=False
,将有额外的 space 和选项justification
将起作用。实际上,它适用于这一整行,而不仅仅是sg.Column
。本行仅最后sg.Column
,设置最终归属justification
. - 对于选项
element_justification
,它适用于此sg.Column
中的所有元素,如果您通过选项expand_x=True
提供更多 space,它将起作用。 - 对于选项
vertical_alignment
,如果有更多 space thissg.Column
在“这一行”的垂直方向上。设置expand_y=False
,所以这个sg.Column
的高度会低于这一行的最大高度,然后就可以了。 - 对于选项
expand_x
和expand_y
,如果为 True,此列将自动向 X/Y 方向扩展以填充可用的 space.
import PySimpleGUI as sg
def make_window(window=None, justification='left', element_justification='left',
vertical_alignment='top', expand_x=True, expand_y=True):
column_layout = [
[sg.Text("(0, 0)", text_color='black', background_color='yellow'),
sg.Text("(0, 1)", text_color='black', background_color='green')],
[sg.Text("(1, 0)", text_color='black', background_color='red'),
sg.Text("(1, 1)", text_color='black', background_color='blue')],
]
layout = [
[
sg.Column([[sg.Text("Fixed Column")]], size=(200, 200), background_color='blue'),
sg.Column(
column_layout,
justification=justification,
element_justification=element_justification,
expand_x=expand_x,
expand_y=expand_y,
vertical_alignment=vertical_alignment,
background_color='gray',
key='Column'),
],
]
radios = []
for group, text in [
("justification", ("left", "center", "right")),
("element_justification", ("left", "center", "right")),
("vertical_alignment", ("top", "center", "bottom")),
("expand_x", (True, False)),
("expand_y", (True, False))]:
row = []
row.append(sg.Text(group, size=(25, 1)))
for i, value in enumerate(text):
row.append(sg.Radio(str(value), group, default=eval(f"{group}==value"), size=(6, 1), enable_events=True, key=(group, value)))
radios.append(row)
layout += radios
win = sg.Window(title="Lights", layout=layout, size=(800, 400), margins=(0, 0), finalize=True)
if window:
window.close()
return win
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 12))
window = make_window()
justification, element_justification, vertical_alignment, expand_x, expand_y = (
'left', 'left', 'top', True, True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif isinstance(event, tuple):
radio = event[0]
if radio == 'justification':
justification = event[1]
elif radio == 'element_justification':
element_justification = event[1]
elif radio == 'vertical_alignment':
vertical_alignment = event[1]
elif radio == 'expand_x':
expand_x = event[1]
elif radio == 'expand_y':
expand_y = event[1]
window = make_window(window, justification=justification,
element_justification=element_justification,
vertical_alignment=vertical_alignment, expand_x=expand_x,
expand_y=expand_y)
window.close()