简体   繁体   English

Python Guizero - 你如何将按钮居中到窗口中间

[英]Python Guizero - How do you centre buttons to the middle of a window

I am coding in python using the guizero library.我正在使用 guizero 库在 python 中编码。 I have created two simple buttons;我创建了两个简单的按钮; one for the menu and one for the stats.一份用于菜单,一份用于统计数据。 I would like to centre the two buttons within the window so that they will be in the top-middle area and sit next to each other side by side.我想将窗口内的两个按钮居中,以便它们位于顶部中间区域并并排坐在一起。

当前结果和期望结果

from guizero import *


def open_menu():
    print("Menu has been opened")
    Menu.disable()
    Stats.enable()


def open_stats():
    print("Stats has been opened")
    Menu.enable()
    Stats.disable()


app = App(layout="grid")
Menu = PushButton(app, command=open_menu, text="Menu", grid=[0, 1])
Stats = PushButton(app, command=open_stats, text="Stats", grid=[1, 1])
Stats.disable()
app.display()

You can do this by putting the buttons in a Box .您可以通过将按钮放在Box 中来做到这一点。

app = App()
center_box = Box(app, layout = "grid")
Menu = PushButton(center_box, command=open_menu, text="Menu", grid=[0, 1])
Stats = PushButton(center_box, command=open_stats, text="Stats", grid=[1, 1])

The buttons inside the box have the grid layout, so display side by side like you want them to, but the box has auto layout, so is centred in the window.框内的按钮具有网格布局,因此可以像您希望的那样并排显示,但框具有自动布局,因此在窗口中居中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM