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

CustomTkinter组件在Tkinter窗口中不显示问题求助

问题解决:CustomTkinter窗口无内容显示

问题出在你创建的checkbox_slider_frame这个CTkFrame组件没有被正确添加到主窗口中。CustomTkinter组件和Tkinter逻辑一致,创建后必须通过grid()、pack()或place()方法完成布局,否则不会在窗口中显示。

修改后的代码:

import tkinter
import tkinter.messagebox 
import customtkinter

customtkinter.set_appearance_mode("System")  
customtkinter.set_default_color_theme("green")

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # 配置窗口
        self.title("Tracking System")
        self.geometry("350x500")
        self.resizable(False,False) # 禁用最小化/最大化按钮

        self.checkbox_slider_frame = customtkinter.CTkFrame(self)
        # 为frame添加布局,这里用pack示例,也可根据需求替换为grid/place
        self.checkbox_slider_frame.pack(pady=20, padx=20, fill="both", expand=True)

        self.switch = customtkinter.CTkSwitch(master=self.checkbox_slider_frame, command=lambda: print("switch 1 toggle"))
        self.switch.grid(row=0, column=0, padx=10, pady=10)
        self.switch.select()
        
        self.button = customtkinter.CTkButton(master=self.checkbox_slider_frame, text="CTkButton")
        self.button.grid(row=1 , column = 1, padx=10, pady=10)

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

关键修改说明:

  • 给self.checkbox_slider_frame添加了pack()布局(若用grid,需同时配置主窗口的grid权重,比如self.grid_rowconfigure(0, weight=1)和self.grid_columnconfigure(0, weight=1),再用self.checkbox_slider_frame.grid(row=0, column=0, sticky="nsew", padx=20, pady=20))
  • 给内部的开关和按钮添加padx、pady参数,优化组件间距
  • 修正了类定义前多余的空格,符合PEP8编码规范

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 19:55:12