tkinter Treeview背景颜色问题

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

我正在使用 Tkinter 和 Customtkinter 库在 python 中创建密码管理器。为了能够查看所有密码,我决定使用树视图方法,但我无法配置其背景颜色。 该代码从登录页面开始,其凭据为: “账户名 - Kami”和 “密码 - 1234”

树形视图是在 home_page() 中创建的

#Password manager

import customtkinter as ctk
import tkinter as tk
from tkinter import ttk

root = ctk.CTk()
root._set_appearance_mode("dark")
root.title("Password Manager")
root.state('zoomed')
ctk.set_default_color_theme("green")

def on_f11(event):
    if root.attributes('-fullscreen'):
        root.attributes('-fullscreen',False)
    else:
        root.attributes('-fullscreen',True)

root.bind('<F11>',on_f11)

def login():
    uid = "kami"
    pas = "1234"
    global frame

    def validate_login():
        if username_entry.get() == uid and pass_entry.get() == pas:
            tk.messagebox.showinfo("Login Successful", f"Welcome {uid}")
            home_page()
        else:
            tk.messagebox.showerror("Login Failed", "Incorrect username or password")

    frame = ctk.CTkFrame(root,width=400,
                        height=600,  
                        corner_radius=0,
                        border_color="black")
    frame.pack(padx=500, pady=175)
    frame.pack_propagate(False)

    label = ctk.CTkLabel(
        master = frame,
        text='Login',
        font=('Khula Light', 36)
        )
    label.pack(pady=20)

    username_label = ctk.CTkLabel(
        master = frame,
        text='Username:',
        font=('Khula Light', 27))
    username_label.pack(padx=42,anchor=tk.W)

    username_entry = ctk.CTkEntry(
        master = frame,
        placeholder_text="User ID",
        width=250,
        height=50
    )
    username_entry.pack(padx=42,anchor=tk.W)

    
    
    pass_label = ctk.CTkLabel(
        master = frame,
        text='Password:',
        font=('Khula Light', 27))
    pass_label.place(relx=0.1,rely=0.45)

    pass_entry = ctk.CTkEntry(
        master = frame,
        placeholder_text="Password",
        width=250,
        height=50,
        corner_radius=10,
        show = '*'
    )
    pass_entry.place(relx=0.1,rely=0.54)

    login_button = ctk.CTkButton(
        master = frame,
        text="Login",
        fg_color=("green"),
        command=validate_login,
        font=("Khula Light",28),)
    login_button.place(relx=0.5,rely=0.8,anchor=tk.CENTER)
    pass_entry.bind('<Return>', lambda event=None: validate_login())

def home_page():
    global manager
    manager = {}
    def add_password():
        def append_pass():
            global manager
            if acc_entry.get() == '':
                tk.messagebox.showerror("Password Manager", "You cannot leave empty fields!")
            elif pass2_entry.get() == '':
                tk.messagebox.showerror("Password Manager", "You cannot leave empty fields!")
            elif web_entry.get() == '':
                tk.messagebox.showerror("Password Manager", "You cannot leave empty fields!")
            else:
                if acc_entry.get().isspace():
                    tk.messagebox.showerror("Password Manager", "You cannot leave empty fields!")
                elif pass2_entry.get().isspace():
                    tk.messagebox.showerror("Password Manager", "You cannot leave empty fields!")
                elif web_entry.get().isspace():
                    tk.messagebox.showerror("Password Manager", "You cannot leave empty fields!")
                else:
                    manager[acc_entry.get()] = [pass2_entry.get(),web_entry.get()]
                    tk.messagebox.showinfo("Password Manager", "Password sucessfully added")
                    print(manager)
                    add_password_frame.destroy()
                    home_page()
        global manager
        home_frame.destroy()
        add_password_frame = ctk.CTkFrame(
            master=root,
            width=400,
            height=600,
            corner_radius=0
            )
        add_password_frame.pack(padx=500, pady=175)
        add_password_frame.pack_propagate(False)

        add_password_label = ctk.CTkLabel(
            master = add_password_frame,
            text='Add New Password',
            font=('Khula Light', 36)
        )
        add_password_label.pack(pady=20)

        acc_entry = ctk.CTkEntry(
            master = add_password_frame,
            placeholder_text="Enter account name",
            width=250,
            height=50
        )
        acc_entry.pack(padx=42,anchor=tk.W)

        web_entry = ctk.CTkEntry(
            master=add_password_frame,
            placeholder_text="Website/App Name",
            width=250,
            height=50,
            corner_radius=10
        )
        web_entry.place(relx=0.1,rely=0.375)

        pass2_entry = ctk.CTkEntry(
            master = add_password_frame,
            placeholder_text="Password",
            width=250,
            height=50,
            corner_radius=10,
            show = '*'
        )
        pass2_entry.place(relx=0.1,rely=0.54)

        add_button = ctk.CTkButton(
            master = add_password_frame,
            text="Add",
            fg_color=("green"),
            command=append_pass,
            font=("Khula Light",28),)
        add_button.place(relx=0.5,rely=0.8,anchor=tk.CENTER)
        pass2_entry.bind('<Return>', lambda event=None: append_pass())

    frame.destroy()
    
    home_frame = tk.Frame(
        root,
        width=1920,
        height=1080,
        background="#121212")
    home_frame.pack()
    home_frame.pack_propagate(False)

    table = ttk.Treeview(
        home_frame, 
        columns= ('Name', 'Website', 'Password'), show= 'headings')
    table.heading('Name', text="Name")
    table.heading('Website', text="Website")
    table.heading('Password', text="Password")
    table.pack(fill="both", expand=True)   

    style = ttk.Style(home_frame)

    style.configure("Treeview", fieldbackground='#121212',background='#121212',foreground='#121212')
    style.configure("Treeview.Heading", font=("Khula Light",27),foreground='#121212') 

    addnew = ctk.CTkButton(
        master=home_frame,
        text="Add Password",
        fg_color=("green"),
        command=add_password,
        font=("Khula Light",28)
    )
    addnew.place(relx=0.45,rely=0.9)
login()
root.mainloop()
python tkinter customtkinter
1个回答
0
投票

将此行添加到您的脚本中

style.theme_use("clam")

它将树视图的背景颜色更改为所需的颜色。

还有一个改进消息框外观的小建议,使它们像 Customtkinter 一样更加现代,请查看这个名为 CTKMessageBox 的开源包。

© www.soinside.com 2019 - 2024. All rights reserved.