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

使用CustomTkinter开发GUI时遇RecursionError:递归深度超出限制

CustomTkinter递归错误修复方案

问题详情

运行CustomTkinter应用时触发递归错误,报错信息:

Traceback (most recent call last):
File "c:\Users\xxx\Desktop\xxx\main.pyw", line 9, in
app = App()
^^^^^
File "c:\Users\xxx\Desktop\xxx\main.pyw", line 6, in init
self.geometry('400x300')
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\customtkinter\windows\ctk_tk.py", line 194, in geometry
super().geometry(self._apply_geometry_scaling(geometry_string))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\customtkinter\windows\widgets\scaling\scaling_base_class.py", line 134, in apply_geometry_scaling
assert self.scaling_type == "window"
^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\tkinter_init
.py", line 2410, in getattr
return getattr(self.tk, attr)
^^^^^^^
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\tkinter_init
.py", line 2410, in getattr
return getattr(self.tk, attr)
^^^^^^^
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\tkinter_init
.py", line 2410, in getattr
return getattr(self.tk, attr)
^^^^^^^
[Previous line repeated 992 more times]
RecursionError: maximum recursion depth exceeded

应用代码:

import customtkinter

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__
        self.geometry('400x300')

if __name__ == '__main__':
    app = App()
    app.mainloop()

尝试修改递归深度至2000后,错误消失但应用无法打开。

修复方法

问题根源是**super().__init__未添加括号**,导致父类构造函数未被执行,CustomTkinter窗口对象未完成初始化,后续调用geometry方法触发递归错误。

修正后的代码:

import customtkinter

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()  # 添加括号,正确调用父类初始化
        self.geometry('400x300')

if __name__ == '__main__':
    app = App()
    app.mainloop()

说明

  • super().__init__()是正确调用父类构造函数的语法,不加括号仅引用方法对象,不会执行初始化逻辑。
  • 父类customtkinter.CTk的初始化会完成窗口底层创建、缩放参数配置等关键操作,缺失这一步会导致所有依赖初始化状态的方法异常。
  • 修改递归深度属于治标不治本,只会掩盖问题,无法解决根本原因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 17:02:40