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

CustomTkinter技术问题:如何将Entry组件置于Frame组件上方?

解决CTkEntry无法显示在CTkFrame上方的问题

你的代码里random_frame和Box1通过grid布局放在了同一行不同列,但random_frame宽度设为480,加上左右padx=10后,几乎占满了500宽度的窗口,导致Box1被挤到窗口外部,所以无法看到——这不是被Frame遮挡,而是布局位置错误。

根据你的需求,提供两种解决方案:

1. 让CTkEntry在CTkFrame垂直上方(上下排列)

修改grid的row参数,将Box1放在random_frame的上一行、同一列:

import customtkinter

app = customtkinter.CTk()
app.geometry("500x380")

random_frame = customtkinter.CTkFrame(master=app, width=480, height=50)
Box1 = customtkinter.CTkEntry(master=app, width=30, height=30)

# Box1放在row2,column0,位于frame上方
Box1.grid(row=2, column=0, padx=10, pady=(10, 0))
random_frame.grid(row=3, column=0, padx=10, pady=(0, 10))

app.mainloop()

2. 让CTkEntry覆盖在CTkFrame上方(层级叠加)

如果需要Box1视觉上覆盖在random_frame上面,有两种实现方式:

方式一:同一grid位置叠加

将两者放在同一row和column,再用lift()确保Entry在层级上层:

import customtkinter

app = customtkinter.CTk()
app.geometry("500x380")

random_frame = customtkinter.CTkFrame(master=app, width=480, height=50)
Box1 = customtkinter.CTkEntry(master=app, width=30, height=30)

random_frame.grid(row=3, column=0, padx=10, pady=10)
Box1.grid(row=3, column=0, padx=10, pady=10)
Box1.lift()  # 将Entry提升到Frame层级上方

app.mainloop()

方式二:将Entry设为Frame的子组件

直接把Box1的master设为random_frame,让它成为Frame的内部组件:

import customtkinter

app = customtkinter.CTk()
app.geometry("500x380")

random_frame = customtkinter.CTkFrame(master=app, width=480, height=50)
random_frame.grid(row=3, column=0, padx=10, pady=10)

# Entry的父组件为Frame,放在Frame内部
Box1 = customtkinter.CTkEntry(master=random_frame, width=30, height=30)
Box1.pack(pady=10, padx=10, anchor="nw")

app.mainloop()

原代码中lift()无效的原因:Box1被挤到了窗口外面,根本不在random_frame的覆盖区域内,所以提升层级也无法显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 05:10:49