将 3 个按钮放置在同一行的左侧、中间和右侧
Position 3 buttons to left, center, and right on same row
我这样做了,但按钮不在同一行:
button_frame = tk.Frame()
button_frame.pack(side = 'top', fill = 'x')
x = tk.Button(button_frame, text = 'x')
x.pack(side = 'left', ipadx = 3, ipady = 1)
y= tk.Button(button_frame, text = 'y')
y.pack(side = 'top', ipadx = 3, ipady = 1)
z= tk.Button(button_frame, text = 'z')
z.pack(side = 'right', ipadx = 3, ipady = 1)
我也尝试用 side = 'left'
+ padx = (0, number)
定位每个按钮,但它只适用于我自己的屏幕分辨率。
这个使用 grid() 的脚本是将三个按钮放在一行中的一种方法,按钮分别固定在左、中和右。 self.columnconfigure 第 0 列和第 2 列的语句对于按比例调整大小不是必需的,但可能对其他小部件布局方法有用。
将 sticky 与 columnspan 和 columnconfigure 控件结合使用,在 window 调整大小时,按钮的位置相对于 button_frame。
import tkinter as tk
class CenterButtons(tk.Tk):
def __init__(self):
super().__init__()
self.grid()
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2, weight=1)
self.xyz_buttons()
@staticmethod
def xyz_buttons():
"""Keep buttons evenly spaced with window resizing."""
button_frame = tk.Frame()
button_frame.grid(row=0, column=0, columnspan=3, sticky=tk.EW)
button_frame.columnconfigure(1, weight=1)
x = tk.Button(button_frame, text='x')
y = tk.Button(button_frame, text='y')
z = tk.Button(button_frame, text='z')
x.grid(row=0, column=0, pady=3, padx=3, sticky=tk.W)
y.grid(row=0, column=1, pady=3, padx=3)
z.grid(row=0, column=2, pady=3, padx=3, sticky=tk.E)
if __name__ == "__main__":
app = CenterButtons()
app.minsize(125, 40)
app.mainloop()
您可以使用 .pack()
获得您想要的内容,但打包按钮的顺序很重要:
x = tk.Button(button_frame, text='x')
y = tk.Button(button_frame, text='y')
z = tk.Button(button_frame, text='z')
# pack left and right first
x.pack(side='left')
z.pack(side='right')
# then pack top
y.pack(side='top')
不过我建议改用grid()
:
x = tk.Button(button_frame, text='x')
y = tk.Button(button_frame, text='y')
z = tk.Button(button_frame, text='z')
button_frame.columnconfigure(1, weight=1)
x.grid(row=0, column=0)
y.grid(row=0, column=1)
z.grid(row=0, column=2)
我这样做了,但按钮不在同一行:
button_frame = tk.Frame()
button_frame.pack(side = 'top', fill = 'x')
x = tk.Button(button_frame, text = 'x')
x.pack(side = 'left', ipadx = 3, ipady = 1)
y= tk.Button(button_frame, text = 'y')
y.pack(side = 'top', ipadx = 3, ipady = 1)
z= tk.Button(button_frame, text = 'z')
z.pack(side = 'right', ipadx = 3, ipady = 1)
我也尝试用 side = 'left'
+ padx = (0, number)
定位每个按钮,但它只适用于我自己的屏幕分辨率。
这个使用 grid() 的脚本是将三个按钮放在一行中的一种方法,按钮分别固定在左、中和右。 self.columnconfigure 第 0 列和第 2 列的语句对于按比例调整大小不是必需的,但可能对其他小部件布局方法有用。
将 sticky 与 columnspan 和 columnconfigure 控件结合使用,在 window 调整大小时,按钮的位置相对于 button_frame。
import tkinter as tk
class CenterButtons(tk.Tk):
def __init__(self):
super().__init__()
self.grid()
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2, weight=1)
self.xyz_buttons()
@staticmethod
def xyz_buttons():
"""Keep buttons evenly spaced with window resizing."""
button_frame = tk.Frame()
button_frame.grid(row=0, column=0, columnspan=3, sticky=tk.EW)
button_frame.columnconfigure(1, weight=1)
x = tk.Button(button_frame, text='x')
y = tk.Button(button_frame, text='y')
z = tk.Button(button_frame, text='z')
x.grid(row=0, column=0, pady=3, padx=3, sticky=tk.W)
y.grid(row=0, column=1, pady=3, padx=3)
z.grid(row=0, column=2, pady=3, padx=3, sticky=tk.E)
if __name__ == "__main__":
app = CenterButtons()
app.minsize(125, 40)
app.mainloop()
您可以使用 .pack()
获得您想要的内容,但打包按钮的顺序很重要:
x = tk.Button(button_frame, text='x')
y = tk.Button(button_frame, text='y')
z = tk.Button(button_frame, text='z')
# pack left and right first
x.pack(side='left')
z.pack(side='right')
# then pack top
y.pack(side='top')
不过我建议改用grid()
:
x = tk.Button(button_frame, text='x')
y = tk.Button(button_frame, text='y')
z = tk.Button(button_frame, text='z')
button_frame.columnconfigure(1, weight=1)
x.grid(row=0, column=0)
y.grid(row=0, column=1)
z.grid(row=0, column=2)