使用customtkinter模板代码报错:无set_appearance_mode属性求助
解决AttributeError: module 'customtkinter' has no attribute 'set_appearance_mode'
问题原因
这个错误是因为你当前安装的customtkinter版本不支持顶层的set_appearance_mode和set_default_color_theme函数——这些函数是在较新的customtkinter版本中才加入的顶层API,旧版本的API结构不同;另外Python3.7属于较老版本,部分新版customtkinter已经不再对其提供支持。
解决方案
1. 检查当前customtkinter版本
在PyCharm的终端中执行以下命令查看版本:
pip show customtkinter
如果版本低于3.0,基本可以确定是版本太旧导致的API不兼容。
2. 升级到兼容Python3.7的customtkinter版本
由于Python3.7已经停止维护,新版customtkinter(如5.x及以上)不再支持它,需要安装支持Python3.7的最高兼容版本。执行以下命令:
pip install customtkinter==4.6.3
这个版本是已知支持Python3.7的较新版本,包含你需要的顶层API函数。
3. 若无法升级,修改代码适配旧版本
如果因为环境限制无法升级,可以调整代码适配旧版本的API逻辑——旧版本中这些配置方法是绑定在CTk实例上的,而非顶层模块函数:
import customtkinter app = customtkinter.CTk() app.geometry("400x240") # 将配置方法改为调用app实例的方法 app.set_appearance_mode("System") app.set_default_color_theme("blue") def button_function(): print("button pressed") button = customtkinter.CTkButton(master=app, text="CTkButton", command=button_function) button.place(relx=0.5, rely=0.5, anchor=customtkinter.CENTER) app.mainloop()
内容的提问来源于stack exchange,提问作者rick
相关产品推荐
相关产品推荐

