如何将两个按钮彼此相邻放置? [英] How can I put 2 buttons next to each other?

查看:76
本文介绍了如何将两个按钮彼此相邻放置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

b = Button(root, text="Enter", width=10, height=2, command=button1)
b.config()
b.pack(side=LEFT)

c = Button(root, text="Clear", width=10, height=2, command=clear)
c.pack(side=LEFT)


scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
text.config(width=35, height=15)
text.pack(side=RIGHT, fill=Y)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)

如何将这两个按钮彼此相邻放置在文本小部件的顶部?当我设置"side = LEFT"时,它只是将2个按钮放在文本小部件旁边

How can I put these 2 buttons next to each other ontop of the text widget? when I set "side=LEFT" it just puts the 2 buttons next to the text widget

推荐答案

这种类型的问题通常有两种解决方案.在所有情况下或在此特定示例中,任何一个都不比另一个要好.两种解决方案都是完全可以接受的.

There are typically two solutions to this type of problem. Neither is better than the other in all circumstances, or in this particular example. Both solutions are perfectly acceptable.

解决方案1:使用两个容器(通常是框架).一种用于容纳水平物品,一种用于容纳垂直物品.在这种情况下,根窗口可以用作垂直堆叠项目的容器.将两个按钮放在水平框架中(使用pack(side = LEFT)),然后将该框架置于文本小部件上方(使用pack(side = TOP)).

Solution 1: use two containers (typically, frames). One to hold horizontal items, one to hold vertical items. In this case the root window can serve as the container for the vertically stacked items. Place the two buttons in the horizontal frame (using pack(side=LEFT)), then put that frame above the text widget (using pack(side=TOP)).

解决方案2:使用网格几何图形管理器,将您的UI布置在网格中.将按钮放在单元格0,1和0,2中,将文本小部件放在1,1中,跨越两列.

Solution 2: use the grid geometry manager, which lays out your UI in a grid. Place the buttons in cells 0,1 and 0,2, and the text widget in 1,1 spanning two columns.

通常,使用网格需要更多的前期计划.您必须弄清楚哪些项目需要跨越列或行,随着小部件的大小调整,哪些列或行应增长和收缩,等等.打包解决方案是最简单的"(无论如何对于"easy"的一些定义)这样的简单布局.

Typically, using grid requires a bit more up-front planning. You have to figure out which items need to span columns or rows, which columns or rows should grow and shrink as the widget is re-sized, etc. The pack solution is "easiest" (for some definitions of "easy", anyway) for very simple layouts such as this.

使用框架包含小部件组的一般技术是一种很好的方法.这样可以轻松地将一组小部件作为一个整体进行管理.并混合和匹配从左到右的窗口小部件组和从上到下的窗口小部件组.

The general technique of using frames to contain groups of widgets is a good one. It makes it easy to manage whole sets of widgets as a group. and to mix and match left-to-right groups of widgets and top-to-bottom groups of widgets.

这就是我使用多帧技术的方法.注意,我是如何将小部件创建为root的子代而不是内部框架的子代的.这样一来,将来修改布局就变得容易一些,因为您要做的就是创建或删除各种框架,而无需修改小部件层次结构.

This is how I would do it using the multiple-frames technique. Notice how I create the widgets as children of root rather than children of the inner frames. This makes it a bit easier to modify the layout in the future, since all you have to do is create or remove various frames without having to modify the widget hierarchy.

# create the main sections of the layout, 
# and lay them out
top = Frame(root)
bottom = Frame(root)
top.pack(side=TOP)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)

# create the widgets for the top part of the GUI,
# and lay them out
b = Button(root, text="Enter", width=10, height=2, command=button1)
c = Button(root, text="Clear", width=10, height=2, command=clear)
b.pack(in_=top, side=LEFT)
c.pack(in_=top, side=LEFT)

# create the widgets for the bottom part of the GUI,
# and lay them out
text = Text(root, width=35, height=15)
scrollbar = Scrollbar(root)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)
scrollbar.pack(in_=bottom, side=RIGHT, fill=Y)
text.pack(in_=bottom, side=LEFT, fill=BOTH, expand=True)

以下是使用网格的幼稚实现.它具有不同的调整大小行为,这可能是您想要的,也可能不是您想要的.这实际上取决于您想要的调整大小行为.通常,我将使用框架管理一行控件,然后使用网格将其与其他小部件一起布置.在此示例中,我将对所有内容使用网格.

A naive implementation using grid is below. It has a different resize behavior which may or may not be what you want. It really depends on the resize behavior that you desire. Usually I'll manage a row of controls with a frame, then use grid to lay it out along with the other widgets. In this example I'll just use grid for everything.

请注意,除了管理小部件所在的行和列之外,还必须确定行和列的加权因子.至少您需要选择一行和一列来弥补松弛",这通常意味着您的主窗口小部件所在的列(通常是文本,画布或其他框架)都在其中.

Notice how in addition to managing which row(s) and column(s) a widget is in, you have to decide on a weighting factor for rows and columns. At a minimum you need to pick one row and one column to "pick up the slack", which usually means whatever column your primary widget is in (read: usually a text, canvas, or another frame) is in.

b = Button(root, text="Enter", width=10, height=2, command=button1)
c = Button(root, text="Clear", width=10, height=2, command=clear)
b.grid(row=0,column=0, sticky=W)
c.grid(row=0,column=1, sticky=W)

textframe = Frame(root)
textframe.grid(in_=root, row=1, column=0, columnspan=3, sticky=NSEW)
root.columnconfigure(0, weight=1)
root.rowconfigure(1, weight=1)

text = Text(root, width=35, height=15)
scrollbar = Scrollbar(root)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)
scrollbar.pack(in_=textframe, side=RIGHT, fill=Y)
text.pack(in_=textframe, side=LEFT, fill=BOTH, expand=True)

在这种特定于 的情况下,我将选择第一种方法,即带包装的方法.对于更复杂的设计,我几乎总是结合使用网格和包.将pack用于自然地水平或垂直堆叠的物品(例如工具栏).将网格用于更复杂的布局(例如整个应用,带有滚动条的小部件,对话框等).您不能在同一个容器中同时使用网格和包,但可以在一个容器中使用pack,在另一个容器中使用网格,等等.

In this specific case I'd choose the first method, the one with pack. For more complex designs I'll almost always use a mix of both grid and pack. Use pack for items that naturally stack either horizontally or vertically (such as toolbars). Use grid for more complex layouts (such as the whole app, widgets with scrollbars, dialogs, etc). You can't use both grid and pack for the same container, but you can use pack for one container, grid for another, etc.

这篇关于如何将两个按钮彼此相邻放置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆