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

如何使CustomTkinter框架背景与主窗口背景颜色匹配?

问题描述

我使用Python的tkinter和customtkinter编写了如下登录界面程序,并设置了dark外观模式(customtkinter.set_appearance_mode('dark')):

import customtkinter


def login():
    if email_entry.get() == "admin" and password_entry.get() == "password":
        message_label.configure(text="Login successful!")
    else:
        message_label.configure(text="Invalid username or password")

def sign_user():
    print('open another page and make the log')

def help_user():
    print('forget password?')

# window
window = customtkinter.CTk()
window.title("Agenda de Rotina")
window.geometry('500x300+710+390')
window.resizable(False, False)
customtkinter.set_appearance_mode('dark')

#create the frame to email and password
frame_1 = customtkinter.CTkFrame(window)
frame_1.pack(pady=(70, 5))

# create the email and password entry widgets
email_label = customtkinter.CTkLabel(frame_1, text="E-mail:")
email_label.grid(row=0, column=0, padx=5, pady=5)
email_entry = customtkinter.CTkEntry(frame_1)
email_entry.grid(row=0, column=1, padx=5, pady=5)

password_label = customtkinter.CTkLabel(frame_1, text="Password:")
password_label.grid(row=1, column=0, padx=5, pady=5)
password_entry = customtkinter.CTkEntry(frame_1)
password_entry.grid(row=1, column=1, padx=5, pady=5)

# create the login button
login_button = customtkinter.CTkButton(window, text="Login", command=login)
login_button.pack(pady = 10)

# create a label for displaying messages
message_label = customtkinter.CTkLabel(window, text="")
message_label.pack()

# create the frame to the sign and help? button
frame_2 = customtkinter.CTkFrame(window)
frame_2.pack(pady = 10)

# create the sign and need help? button
sign_button = customtkinter.CTkButton(frame_2, text = 'Sign', width = 100, command = sign_user)
sign_button.grid(row = 0, column = 0, padx = (0, 10))
help_button = customtkinter.CTkButton(frame_2, text = 'Help?', width = 100, command = help_user)
help_button.grid(row = 0, column = 1, padx = (10, 0))


window.mainloop()

我希望让界面中的CTkFrame框架背景颜色与主窗口背景保持一致,但尝试设置时未能成功:
我定义了颜色变量my_window_dark = '#242424'并设置frame_1 = customtkinter.CTkFrame(window, bg_color='my_window_dark'),出现报错;直接设置随机颜色时,框架出现了异常的边框效果(框架异常边框效果)。
请求解决如何使CTkFrame背景与主窗口背景颜色匹配的问题。

解决方法

错误原因

  1. 参数名错误:CTkFrame的背景色参数是fg_color而非bg_color;
  2. 变量传递错误:你把字符串'my_window_dark'传给了参数,而非变量my_window_dark;
  3. 边框未隐藏:默认的CTkFrame带有边框,即使颜色匹配也会出现突兀的边框效果。

正确实现方式

不需要硬编码颜色值,直接动态获取主窗口的背景色,同时隐藏框架边框:

# 获取当前窗口的背景色(适配dark/light模式)
window_bg = window.cget("fg_color")

# 创建匹配背景的frame_1
frame_1 = customtkinter.CTkFrame(window, fg_color=window_bg, border_width=0)
frame_1.pack(pady=(70, 5))

# 创建匹配背景的frame_2
frame_2 = customtkinter.CTkFrame(window, fg_color=window_bg, border_width=0)
frame_2.pack(pady=10)

将上述代码替换原代码中创建frame_1和frame_2的部分即可。这样设置后,CTkFrame的背景会和主窗口完全一致,同时去掉边框,不会出现异常效果。

完整修改后的代码

import customtkinter


def login():
    if email_entry.get() == "admin" and password_entry.get() == "password":
        message_label.configure(text="Login successful!")
    else:
        message_label.configure(text="Invalid username or password")

def sign_user():
    print('open another page and make the log')

def help_user():
    print('forget password?')

# window
window = customtkinter.CTk()
window.title("Agenda de Rotina")
window.geometry('500x300+710+390')
window.resizable(False, False)
customtkinter.set_appearance_mode('dark')

# 获取当前窗口背景色
window_bg = window.cget("fg_color")

#create the frame to email and password
frame_1 = customtkinter.CTkFrame(window, fg_color=window_bg, border_width=0)
frame_1.pack(pady=(70, 5))

# create the email and password entry widgets
email_label = customtkinter.CTkLabel(frame_1, text="E-mail:")
email_label.grid(row=0, column=0, padx=5, pady=5)
email_entry = customtkinter.CTkEntry(frame_1)
email_entry.grid(row=0, column=1, padx=5, pady=5)

password_label = customtkinter.CTkLabel(frame_1, text="Password:")
password_label.grid(row=1, column=0, padx=5, pady=5)
password_entry = customtkinter.CTkEntry(frame_1)
password_entry.grid(row=1, column=1, padx=5, pady=5)

# create the login button
login_button = customtkinter.CTkButton(window, text="Login", command=login)
login_button.pack(pady = 10)

# create a label for displaying messages
message_label = customtkinter.CTkLabel(window, text="")
message_label.pack()

# create the frame to the sign and help? button
frame_2 = customtkinter.CTkFrame(window, fg_color=window_bg, border_width=0)
frame_2.pack(pady = 10)

# create the sign and need help? button
sign_button = customtkinter.CTkButton(frame_2, text = 'Sign', width = 100, command = sign_user)
sign_button.grid(row = 0, column = 0, padx = (0, 10))
help_button = customtkinter.CTkButton(frame_2, text = 'Help?', width = 100, command = help_user)
help_button.grid(row = 0, column = 1, padx = (10, 0))


window.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 02:32:49