简体   繁体   English

在 Python 中设置按钮的位置?

[英]Setting the position on a button in Python?

I just wrote a code that creates a window (using TKinter) and displays one working button.我刚刚编写了一个代码来创建一个窗口(使用 TKinter)并显示一个工作按钮。

b = Button(master, text="get", width=10, command=callback)

But i would like to have multiple buttons underneath this one.但我想在这个下面有多个按钮。

How do you set the row and column of the button?你如何设置按钮的行和列? I tried to add row = 0, column = 0, but that would not work.我试图添加row = 0, column = 0,但这不起作用。

Thanks谢谢

Causing a widget to appear requires that you position it using with what Tkinter calls "geometry managers".使小部件出现需要您使用 Tkinter 所谓的“几何管理器”来定位它。 The three managers are grid , pack and place .这三个管理器是gridpackplace Each has strengths and weaknesses.每个人都有优点和缺点。 These three managers are implemented as methods on all widgets.这三个管理器被实现为所有小部件上的方法。

grid , as its name implies, is perfect for laying widgets in a grid. grid ,顾名思义,非常适合在网格中放置小部件。 You can specify rows and columns, row and column spans, padding, etc.您可以指定行和列、行和列的跨度、填充等。

Example:例子:

b = Button(...)
b.grid(row=2, column=3, columnspan=2)

pack uses a box metaphor, letting you "pack" widgets along one of the sides of a container. pack使用盒子比喻,让您可以沿着容器的一侧“打包”小部件。 pack is extremely good at all-vertical or all-horizontal layouts. pack 非常擅长全垂直或全水平布局。 Toolbars, for example, where widgets are aligned in a horizontal line, are a good place to use pack.例如,工具栏(widgets 在水平线上对齐)是使用 pack 的好地方。

Example:例子:

b = Button(...)
b.pack(side="top", fill='both', expand=True, padx=4, pady=4)`

place is the least used geometry manager. place是最少使用的几何管理器。 With place you specify the exact x/y location and exact width/height for a widget.使用 place 您可以为小部件指定确切的 x/y 位置和确切的宽度/高度。 It has some nice features such as being able to use either absolute or relative coordinates (for example: you can place a widget at 10,10, or at 50% of the widgets width or height).它有一些不错的功能,例如能够使用绝对坐标或相对坐标(例如:您可以将小部件放置在 10,10,或小部件宽度或高度的 50%)。

Unlike grid and pack , using place does not cause the parent widget to expand or collapse to fit all of the widgets that have been placed inside.gridpack不同,使用place不会导致父小部件展开或折叠以适合已放置在其中的所有小部件。

Example:例子:

b = Button(...)
b.place(relx=.5, rely=.5, anchor="c")

With those three geometry managers you can do just about any type of layout you can imagine.使用这三个几何管理器,您几乎可以执行您可以想象的任何类型的布局。

astynax is right. astynax 是对的。 To follow the example you gave:按照你给出的例子:

MyButton1 = Button(master, text="BUTTON1", width=10, command=callback)
MyButton1.grid(row=0, column=0)

MyButton2 = Button(master, text="BUTTON2", width=10, command=callback)
MyButton2.grid(row=1, column=0)

MyButton3 = Button(master, text="BUTTON3", width=10, command=callback)
MyButton3.grid(row=2, column=0)

Should create 3 row of buttons.应该创建 3 行按钮。 Using grid is a lot better than using pack.使用 grid 比使用 pack 好很多。 However, if you use grid on one button and pack on another it will not work and you will get an error.但是,如果您在一个按钮上使用网格并在另一个按钮上打包,它将不起作用并且您将收到错误消息。

Try Grid Geometry Manager :尝试网格几何管理器

btns = [
    (lambda ctl: ctl.grid(row=r, column=c) or ctl)(
        Button(text=str(1 + r * 3 + c)))
    for c in (0,1,2) for r in (0,1,2)]

result:结果:

[1][2][3]
[4][5][6]
[7][8][9]

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

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