ane*_*yzm 1 python layout tkinter
我是tkinter的新手,我想知道我是否可以在同一行上显示一系列小部件,而不是将它们放在一列中的另一行之下.
我目前正在使用框架来放置我的组件,但是如果我在一个框架中有几个小部件(按钮),我宁愿直接放置按钮,而不是创建其他子框架.
您可以使用几何管理器在容器中布置小部件.Tkinter的几何管理器是网格,包装和放置.
grid允许您按行和列布置小部件.pack允许您沿着盒子的两侧布置小部件(非常适合制作单个水平或垂直列).地方让你使用绝对和相对定位.在实践中,很少使用地方.
在您的情况下,您希望创建一个水平的按钮行,通常通过创建表示行的框架,然后使用pack来并排放置小部件.不要害怕使用子帧进行布局 - 这正是它们的用途.
例如:
import Tkinter as tk
class App:
def __init__(self):
self.root = tk.Tk()
# this will be the container for a row of buttons
# a background color has been added just to make
# it stand out.
container = tk.Frame(self.root, background="#ffd3d3")
# these are the buttons. If you want, you can make these
# children of the container and avoid the use of "in_"
# in the pack command, but I find it easier to maintain
# code by keeping my widget hierarchy shallow.
b1 = tk.Button(text="Button 1")
b2 = tk.Button(text="Button 2")
b3 = tk.Button(text="Button 3")
# pack the buttons in the container. Since the buttons
# are children of the root we need to use the in_ parameter.
b1.pack(in_=container, side="left")
b2.pack(in_=container, side="left")
b3.pack(in_=container, side="left")
# finally, pack the container in the root window
container.pack(side="top", fill="x")
self.root.mainloop()
if __name__ == "__main__":
app=App()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1522 次 |
| 最近记录: |