You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tkinter的PanedWindow中实现子控件左对齐或右对齐

解决方法

你报错的原因是sticky是PanedWindow的add()方法的配置参数,不是子控件(Checkbutton、Label等)本身的初始化参数,不能写在控件构造函数里。

你只需要把sticky参数放到add()方法的入参中即可,修改后的代码如下:

from tkinter import *

root = Tk()

root.resizable()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (300 / 2))
y_cordinate = int((screen_height / 2) - (200 / 2))
root.geometry("{}x{}+{}+{}".format(300, 200, x_cordinate, y_cordinate))
root.title("Pane Sticky Test")

main_pane = PanedWindow(root, orient=VERTICAL)
main_pane.pack(fill=BOTH, expand=1)

var = IntVar()

check = Checkbutton(main_pane, text="My option", variable=var)
# 左对齐传入sticky=W即可
main_pane.add(check, sticky=W)

label = Label(main_pane, text="Preview:")
# 左对齐
main_pane.add(label, sticky=W)

entry = Text(main_pane, height=10, width=50)
# 如果需要输入框填满宽度可以传sticky=EW,保持默认居中就不传该参数
main_pane.add(entry, sticky=EW)
root.mainloop()

sticky参数说明

sticky的取值和grid方法完全一致,可根据需求自由选择:

  • W:左对齐
  • E:右对齐
  • CENTER:居中(默认值)
  • EW:左右拉伸填满当前窗格宽度
  • 支持多值组合,比如W+N表示左上对齐

内容的提问来源于stack exchange,提问作者bookofproofs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 10:42:03