如何将我的框架的颜色背景与主窗口相匹配

问题描述 投票:0回答:2

这就是使用 tkinter 和 customtkinter 的 python 程序:

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()

我用了这个appearance_mode:

customtkinter.set_appearance_mode('dark')

但我想让我的框架背景颜色与我的 bg 窗口相同。我试过设置,但不起作用。

my_window_dark = '#242424'

# and i put the variable in the frame
frame_1 = customtkinter.CTkFrame(window, bg_color = 'my_window_dark')
frame_1.pack(pady=(70, 5))

当我随便放一种颜色看看会发生什么时:

我试图为颜色创建一个变量,然后他们将该变量放入框架的颜色方法中,但出现错误。另外,当我在库中放置一个随机颜色时,会出现我放在上面的图像

python tkinter background-color customtkinter
2个回答
0
投票

要使框架的颜色与父窗口的颜色匹配,您需要获取父窗口的颜色并将框架的前景色更改为它。

my_window_dark = window.cget("bg")

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

0
投票

你可以简单地使用特殊颜色“透明”:

frame_1 = customtkinter.CTkFrame(window, fg_color="transparent")
© www.soinside.com 2019 - 2024. All rights reserved.