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

huangapple go评论61阅读模式
英文:

how to match the color back ground of my frame with the master window

问题

以下是你的Python代码的翻译部分:

import customtkinter

def login():
    if email_entry.get() == "admin" and password_entry.get() == "password":
        message_label.configure(text="登录成功!")
    else:
        message_label.configure(text="无效的用户名或密码")

def sign_user():
    print('打开另一个页面并记录日志')

def help_user():
    print('忘记密码?')

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

# 创建包含电子邮件和密码的框架
frame_1 = customtkinter.CTkFrame(window)
frame_1.pack(pady=(70, 5))

# 创建电子邮件和密码输入小部件
email_label = customtkinter.CTkLabel(frame_1, text="电子邮件:")
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_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)

# 创建登录按钮
login_button = customtkinter.CTkButton(window, text="登录", command=login)
login_button.pack(pady=10)

# 创建用于显示消息的标签
message_label = customtkinter.CTkLabel(window, text="")
message_label.pack()

# 创建包含签名和需要帮助?按钮的框架
frame_2 = customtkinter.CTkFrame(window)
frame_2.pack(pady=10)

# 创建签名和需要帮助?按钮
sign_button = customtkinter.CTkButton(frame_2, text='签名', width=100, command=sign_user)
sign_button.grid(row=0, column=0, padx=(0, 10))
help_button = customtkinter.CTkButton(frame_2, text='需要帮助?', width=100, command=help_user)
help_button.grid(row=0, column=1, padx=(10, 0))

window.mainloop()

关于你尝试更改框架背景颜色的问题,你应该将颜色设置为变量而不是字符串,像这样:

my_window_dark = '#242424'

# 将变量应用于框架的背景颜色
frame_1 = customtkinter.CTkFrame(window, bg_color=my_window_dark)
frame_1.pack(pady=(70, 5))

这应该可以使框架的背景颜色与窗口背景颜色相同。希望这能帮助你解决问题。

英文:

thats the program in python using tkinter and customtkinter:

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

and i used this appearance_mode:

customtkinter.set_appearance_mode('dark')

but i want to to my frame background color be the same as my bg window. I have tried to set, but doesn't work.

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

and when i just put a random color to see what hapens:

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

i tried to creat a variable to the color and them put that variable in the color method of the frame, and get an error. Also when i put a random color that is in the lib, appear the image that i put above

答案1

得分: 1

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

my_window_dark = window.cget("bg")

# 创建用于电子邮件和密码的框架
frame_1 = customtkinter.CTkFrame(window, fg_color=my_window_dark)
frame_1.pack(pady=(70, 5))
英文:

To match the color of the frame with the color of the parent window, you need to get the color of the parent and change the foreground color of the frame to it.

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

答案2

得分: 1

你可以简单地使用特殊颜色 "transparent":

frame_1 = customtkinter.CTkFrame(window, fg_color="transparent")
英文:

You can simply use the special color "transparent":

frame_1 = customtkinter.CTkFrame(window, fg_color="transparent")

huangapple
  • 本文由 发表于 2023年5月14日 15:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定