如何在 PySimpleGUI 中水平而不是垂直添加元素?

发布于 2025-01-18 10:51:05 字数 1293 浏览 2 评论 0原文

我正在尝试让我的GUI拥有2个“零件”(一个左而一个),自然而然地,在布局中添加元素只是垂直扩展,所以我尝试执行“ left_part”布局和“ right_part”布局的“ left_part”布局,然后将它们与水平分离器填充合并,但这给了我完全不同的东西。

理想情况下,我想要这样的东西(也许在之间也有细线):

https://i.sstatic.net/eucxi.png“ alt =”在此处输入图像说明”>

我得到的是:

这是代码:

import PySimpleGUI as sg

left_part = [

    [sg.Text("Pick your favorite fruit")],
    [sg.Input(size=(25, 1), key="path")],
    [sg.FileBrowse(key="fav"), sg.Button("Submit")],
    [sg.Image(key="img1")],

    [sg.Text("Pick your favorite number")],
    [sg.Input(key="num", size=(10,10))],
    [sg.Button("Submit")],

]

right_part = [
    [sg.Text("Pick your favorite animal")],
    [sg.Input(key="animal")],
    [sg.Button("Show image of animal")],
    [sg.Image(key="img2")]
]

layout = [
    [left_part],
    [sg.HSeparator(pad=(500,0))],
    [right_part],
]

window = sg.Window("Favorites", layout)

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

I'm trying to get my GUI to have 2 "parts" (one left and one right), and naturally, adding elements to the layout just expands it vertically, so I tried doing a "left_part" layout and a "right_part" layout, then merge them with a Horizontal Separator padding, but it gives me something entirely different.

Ideally, I would want something like this (maybe with a thin line in between too):

enter image description here

What I get is this:
enter image description here

This is the code:

import PySimpleGUI as sg

left_part = [

    [sg.Text("Pick your favorite fruit")],
    [sg.Input(size=(25, 1), key="path")],
    [sg.FileBrowse(key="fav"), sg.Button("Submit")],
    [sg.Image(key="img1")],

    [sg.Text("Pick your favorite number")],
    [sg.Input(key="num", size=(10,10))],
    [sg.Button("Submit")],

]

right_part = [
    [sg.Text("Pick your favorite animal")],
    [sg.Input(key="animal")],
    [sg.Button("Show image of animal")],
    [sg.Image(key="img2")]
]

layout = [
    [left_part],
    [sg.HSeparator(pad=(500,0))],
    [right_part],
]

window = sg.Window("Favorites", layout)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2025-01-25 10:51:06

布局采用以下形式:leftseparatorright 垂直或按行。

layout = [
    [element1],
    [sg.HSeparator(pad=(500,0))],
    [element2],
]

应该是这样的

layout = [
    [element1, sg.HSeparator(pad=(500,0)), element2],
]

由于 element1 和 element2 用于另一个复杂布局,因此使用 Frame 或 Column 元素。

对于水平布局,此处将使用 VSeparator,而不是 HSeparator

对于 Column 中的元素垂直对齐顶部,因此添加了选项 vertical_alignment='top'

因此,代码中的 layout 可能如下所示,

layout = [
    [sg.Column(left_part, vertical_alignment='top'), sg.VSeparator(), sg.Column(right_part, vertical_alignment='top')],
]

在此处输入图像描述

The layout in following form left, separator and right vertically or in rows.

layout = [
    [element1],
    [sg.HSeparator(pad=(500,0))],
    [element2],
]

Should be like this

layout = [
    [element1, sg.HSeparator(pad=(500,0)), element2],
]

Since the element1 and element2 are for another complex layout, use Frame or Column element.

For horizontal layout, Instead of HSeparator, VSeparator will be used here.

For elements in Column vertical aligned top, so option vertical_alignment='top' added.

So the layout in your code maybe like this,

layout = [
    [sg.Column(left_part, vertical_alignment='top'), sg.VSeparator(), sg.Column(right_part, vertical_alignment='top')],
]

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文