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

CustomTkinter代码中按钮与输入框不可见问题求助

CustomTkinter界面控件功能正常但不可见的问题解决

问题描述

运行以下CustomTkinter登录界面代码时,窗口可正常打开,按钮功能正常(鼠标悬停在对应位置点击能触发打印Login test),但按钮、输入框、标签等所有UI元素均不可见,界面仅显示一个空白的深色矩形窗口。已执行pip install --upgrade customtkinter,问题仍未解决。

相关代码

import customtkinter

customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")

root = customtkinter.CTk()
root.geometry("500x350")

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

label = customtkinter.CTkLabel(master=frame, text="Login System", font=("Roboto", 24))
label.pack(pady=12, padx=10)

entry1 = customtkinter.CTkEntry(master=frame, placeholder_text="Username")
entry1.pack(pady=12, padx=10)

entry2 = customtkinter.CTkEntry(master=frame, placeholder_text="Password", show="*")
entry2.pack(pady=12, padx=10)


def login():
    print("Login test")


button = customtkinter.CTkButton(master=frame, text="Login", command=login)
button.pack(pady=12, padx=10)

checkbox = customtkinter.CTkCheckBox(master=frame, text="Remember Me")
checkbox.pack(pady=12, padx=10)

root.mainloop()

解决思路

  • 检查Python版本兼容性
    CustomTkinter要求Python 3.8及以上版本,若你的Python版本过低,会导致渲染异常。运行python --version查看版本,低于3.8则升级Python。
  • 显式指定控件颜色属性
    部分环境下主题颜色加载失败,会导致控件颜色与背景色一致,看起来不可见。可以给控件手动设置颜色:
    # 修改标签
    label = customtkinter.CTkLabel(master=frame, text="Login System", font=("Roboto", 24), text_color="white")
    # 修改按钮
    button = customtkinter.CTkButton(master=frame, text="Login", command=login, fg_color="#0078D7", text_color="white")
    # 修改输入框
    entry1 = customtkinter.CTkEntry(master=frame, placeholder_text="Username", placeholder_text_color="gray", text_color="white")
    
  • 修复高DPI显示问题(Windows系统)
    高DPI显示器的缩放比例可能导致渲染失效,在代码开头添加以下代码:
    import ctypes
    ctypes.windll.shcore.SetProcessDpiAwareness(1)
    
    或者将系统显示缩放调整为100%后重试。
  • 切换外观模式与主题
    尝试更换外观模式或内置主题,看是否能正常显示:
    customtkinter.set_appearance_mode("light")
    customtkinter.set_default_color_theme("blue")
    
  • 清理缓存后重装CustomTkinter
    卸载现有版本并清理缓存后重新安装:
    pip uninstall customtkinter -y
    pip install customtkinter --no-cache-dir
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 19:06:16