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

如何修改Windows10下CustomTkinter窗口标题栏的背景与文字颜色

解决方案:修改CustomTkinter标题栏颜色

CustomTkinter默认使用Windows原生标题栏,这类系统级控件无法通过常规CustomTkinter API直接修改样式。下面提供两种可行方案:

方案一:自定义标题栏(推荐,样式完全可控)

隐藏原生标题栏,用CustomTkinter组件手动实现标题栏,可完全自定义背景色、文字色和按钮样式。

修改你的类代码如下:

import customtkinter
from tkinter import Event

class FenetreLogin(customtkinter.CTk):
    def __init__(self):
        # 隐藏原生标题栏,启用自定义标题栏模式
        super().__init__(fg_color="white")  # 主窗口背景色可按需调整
        self.overrideredirect(True)  # 关闭原生窗口装饰
        
        # 设置窗口大小并居中
        self.geometry("700x680")
        self.center_window()
        
        # 创建自定义标题栏
        self.title_bar = customtkinter.CTkFrame(self, fg_color="#2c3e50", height=40)
        self.title_bar.pack(fill="x", side="top")
        
        # 标题标签
        self.title_label = customtkinter.CTkLabel(
            self.title_bar, 
            text="Exportation_Fanny v1", 
            text_color="white",
            font=("", 14)
        )
        self.title_label.pack(side="left", padx=10, pady=8)
        
        # 关闭按钮
        self.close_btn = customtkinter.CTkButton(
            self.title_bar, 
            text="×", 
            width=30, 
            height=30,
            fg_color="#e74c3c",
            hover_color="#c0392b",
            command=self.destroy
        )
        self.close_btn.pack(side="right", padx=5, pady=5)
        
        # 最小化按钮
        self.minimize_btn = customtkinter.CTkButton(
            self.title_bar, 
            text="−", 
            width=30, 
            height=30,
            fg_color="#f39c12",
            hover_color="#e67e22",
            command=self.minimize_window
        )
        self.minimize_btn.pack(side="right", padx=5, pady=5)
        
        # 绑定标题栏拖动事件
        self.title_bar.bind("<ButtonPress-1>", self.start_drag)
        self.title_bar.bind("<B1-Motion>", self.drag_window)
        
        # 原有组件
        self.initial_width = 650
        labelAuteur = customtkinter.CTkLabel(master=self,font=("",20,'underline'),text="Made By Lucas Desperrois")
        labelAuteur.pack(anchor="n",pady=10)
        self.resizable(width=False,height=False)
        # self.fenetreConnexion = self.frameConnexion()  # 保留原有方法
        self.browser1=None
        
    def center_window(self):
        # 窗口居中辅助方法
        self.update_idletasks()
        width = self.winfo_width()
        height = self.winfo_height()
        x = (self.winfo_screenwidth() // 2) - (width // 2)
        y = (self.winfo_screenheight() // 2) - (height // 2)
        self.geometry(f"{width}x{height}+{x}+{y}")
        
    def minimize_window(self):
        # 最小化窗口方法
        self.iconify()
        
    def start_drag(self, event: Event):
        # 记录拖动起始位置
        self.drag_x = event.x
        self.drag_y = event.y
        
    def drag_window(self, event: Event):
        # 计算窗口新位置
        x = self.winfo_x() + (event.x - self.drag_x)
        y = self.winfo_y() + (event.y - self.drag_y)
        self.geometry(f"+{x}+{y}")

方案二:通过Windows API修改原生标题栏颜色

如果坚持使用原生标题栏,可调用Windows系统API修改标题栏背景色,但文字颜色受系统主题限制,无法直接修改。

示例代码(需先安装pywin32:pip install pywin32):

import customtkinter
import win32gui
import win32con
from ctypes import windll, c_int, sizeof, byref

class FenetreLogin(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        
        self.geometry("700x680")
        self.title("Exportation_Fanny v1")
        self.initial_width = 650
        labelAuteur = customtkinter.CTkLabel(master=self,font=("",20,'underline'),text="Made By Lucas Desperrois")
        labelAuteur.pack(anchor="n",pady=10)
        self.resizable(width=False,height=False)
        # self.fenetreConnexion = self.frameConnexion()
        self.browser1=None
        
        # 修改原生标题栏背景色为深蓝色
        self.change_title_bar_color("#2c3e50")
        
    def change_title_bar_color(self, hex_color):
        # 十六进制颜色转BGR格式(Windows系统要求)
        hex_color = hex_color.lstrip('#')
        r = int(hex_color[0:2], 16)
        g = int(hex_color[2:4], 16)
        b = int(hex_color[4:6], 16)
        color = (b << 16) | (g << 8) | r
        
        # 获取窗口句柄
        hwnd = win32gui.GetParent(self.winfo_id())
        
        # 调用DWM API设置标题栏颜色
        DWMWA_CAPTION_COLOR = 35
        windll.dwmapi.DwmSetWindowAttribute(
            hwnd,
            DWMWA_CAPTION_COLOR,
            byref(c_int(color)),
            sizeof(c_int)
        )
        
        # 刷新窗口生效
        win32gui.RedrawWindow(hwnd, None, None, win32con.RDW_FRAME | win32con.RDW_INVALIDATE)

注意:方案二仅能修改标题栏背景色,标题文字颜色受系统主题约束,若系统为浅色主题可能出现文字看不清的情况,因此更推荐方案一。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 02:10:36