为何该场景下CTkLabel.place()不接受self=None参数?
问题解决:CustomTkinter TypeError: super(type, obj): obj (instance of NoneType) is not an instance or subtype of type
错误原因
你直接通过ctk.CTkLabel类调用place方法,同时传入self=None,这完全不符合CustomTkinter的组件创建逻辑:
place是组件的实例方法,必须先创建CTkLabel的实例,再调用实例的place方法- 组件必须指定父容器(即你的
App实例),不能传self=None,否则组件无法依附到窗口上,触发类型错误
修正后的代码
for i in range(5): if comparisonGuess[i] == word[i]: print(f"{guess[i]} is in the word at position {i+1}. hooray") # 1. 先创建CTkLabel实例,指定父容器为App实例(self),设置文本和背景色 correct_label = ctk.CTkLabel(master=self, text=guess[i], bg_color="green") # 2. 调用实例的place方法完成布局 correct_label.place(x=120+(i*30), y=guessCount*25)
关键说明
master=self:必须将你的App实例(self)作为组件的父容器,这样标签才能显示在窗口内- CustomTkinter中背景色参数是
bg_color,不是原生Tkinter的bg,注意参数名对应 - 所有GUI组件都需要先实例化,再调用布局方法(
place/pack/grid),不能直接通过类调用布局方法
内容的提问来源于stack exchange,提问作者skittles lover
相关产品推荐
相关产品推荐

