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

如何在Custom Tkinter的CTkScrollableFrame中添加CTkLabel

问题解决:CTkScrollableFrame中无法显示名字列表

你的代码存在一个Tkinter系列库的典型使用误区:
创建CTkScrollableFrame时,你把pack()方法的返回值赋值给了other_frame,但pack()方法的返回结果是None,这导致后续创建的CTkLabel实际是以None作为父组件(最终默认挂载到根窗口),而非你期望的滚动框架。

修正后的代码如下:

import customtkinter

list_names = ["Liam", "Olivia", "Noah", "Emma"]

root = customtkinter.CTk()
root.geometry("1000x550")
root.title("Test")

frame = customtkinter.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)

# 拆分组件实例化与布局操作
other_frame = customtkinter.CTkScrollableFrame(master=frame, height=350, width=250, label_text="List")
other_frame.pack(pady=40)

for name in list_names:
        customtkinter.CTkLabel(master=other_frame, text=name).pack(pady=10)

root.mainloop()

核心修正点是将组件创建和布局方法(pack/grid/place)分开执行,这样other_frame会正确指向CTkScrollableFrame的实例,后续的标签就能被正确添加到滚动框架内部。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 11:02:05