[英]Custom tkinter label background
我正在做一個學校項目,現在我對標簽的背景有疑問。
目的是使標簽的背景與深色和淺色主題中的框架相同,如果可能,請刪除按鈕邊緣的微小差異顏色。
import tkinter import customtkinter root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window root_tk.geometry("1280x720") root_tk.minsize(1280, 720) class GUI: def __init__(self, root_tk): Frame1 = customtkinter.CTkFrame(root_tk) Frame1.pack frame2 = customtkinter.CTkFrame(Frame1, width=100, height=720) frame2.pack(pady=20,padx=20) frame3 = customtkinter.CTkFrame(root_tk, width=250, height=725) frame3.pack() frame3.place(anchor="w", relx=0.0, rely=0.5, relwidth=0.2, relheight=1) self.test() def test(self): self.switch = customtkinter.CTkSwitch(master=root_tk, text="Dark Mode", command=self.theme_change) self.switch.toggle(1) self.switch.place(relx=0.05, rely=0.05) self.label_width = customtkinter.CTkLabel(root_tk, text="glasses width:") self.label_width.place(relx=0.1, rely=0.67, anchor=tkinter.CENTER) self.label_height = customtkinter.CTkLabel(root_tk, text="glasses height:") self.label_height.place(relx=0.1, rely=0.77, anchor=tkinter.CENTER) self.button_add_Face = customtkinter.CTkButton(root_tk, width=200, height=50, border_width=0, corner_radius=8, hover=True, text="Adicionar Rostos", command=print("added")) self.button_add_Face.place(relx=0.1, rely=0.6, anchor=tkinter.CENTER) def theme_change(self): if self.switch.get() == 1: customtkinter.set_appearance_mode("dark") else: customtkinter.set_appearance_mode("light") start = GUI(root_tk) root_tk.mainloop()
根據小部件的CTkBaseClass
的源代碼。 只要您沒有為您的小部件明確定義bg_color
olor,背景顏色應該與您的主控的fg_color
相同。
查看BaseClass
的configure方法並跟隨它到detect_color_of_master 。
目的是使標簽的背景與深色和淺色主題中的框架相同
所以這應該可以解決問題:
Frame1 = customtkinter.CTkFrame(root_tk,fg_color=root_tk.fg_color) Frame1.pack() self.button_add_Face = customtkinter.CTkButton(Frame1, width=200, height=50, border_width=0, corner_radius=8, hover=True, text="Adicionar Rostos", command=print("added"), border_color=root_tk.fg_color)
但您可能對ThemeManager更感興趣。 有一些主題,您可以以示例的形式制作另一個.json
文件,並將其添加到此目錄中以應用您自己的樣式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.