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

CustomTkinter 5.2.0中place方法传width参数报错求助

解决CustomTkinter 5.2.0中place方法无法设置width/height的问题

CustomTkinter对place方法做了封装限制,不允许在place调用中直接传入width和height参数,必须通过组件构造器或configure方法来设置尺寸。

修正后的代码示例

import customtkinter as ctk

# 初始化窗口
window = ctk.CTk()
window.geometry('400x300')

# 在构造器中设置框架初始宽度
frame = ctk.CTkFrame(window, width=20)
# 仅用place方法做定位操作,移除width参数
frame.place(relx=0.5, rely=0.5, anchor='center')

# 后续需调整框架大小时,使用configure方法
# 示例:绑定窗口尺寸变化事件,动态调整框架宽度
def adjust_frame(event):
    # 这里可根据窗口尺寸编写自定义大小计算逻辑
    new_width = event.width // 15
    frame.configure(width=new_width)

window.bind('<Configure>', adjust_frame)

window.mainloop()

关键说明

  • 你之前同时在构造器和place传width无效,是因为CustomTkinter的place封装逻辑会直接拦截并报错width参数,必须彻底移除place中的width/height参数。
  • 如果初始不需要固定尺寸,也可以先构造空框架,再用frame.configure(width=xxx, height=xxx)设置尺寸后调用place定位,顺序不影响。
  • 动态调整尺寸完全依赖configure方法,配合窗口的<Configure>事件可以实现窗口与框架的自定义大小关联,满足你的特殊需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 12:24:56