python-3.x 我无法在tkinter中更改按钮的背景

mm9b1k5b  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(175)

我试图使tkinter项目中的按钮看起来像bootstrap成功按钮,但我没有成功地改变它的颜色。任何额外的美化代码也将不胜感激。change button colour to look more green and positive
我试着改变按钮的每个属性来解决这个问题,但是无法改变颜色的bg。我也试着使用样式的方式而不是配置的方式,但是它使按钮在某种程度上变得更糟。

下面提交的是我使用过的代码

import hashlib
import tkinter as tk
from tkinter import font

# Name of the file containing the passwords
password_file_name = "rockyou.txt"

def md5_hash(password):
    return hashlib.md5(password.encode()).hexdigest()

def crack_password(hash_value, password_list):
    for password in password_list:
        if md5_hash(password) == hash_value:
            return password
    return None

def check_password():
    # Get the password from the user
    password = password_entry.get()

    # Hash the password
    hash_value = md5_hash(password)

    # Crack the password
    password_list = open(password_file_name, "r", encoding='utf-8', errors='ignore').readlines()
    password_list = list(map(str.strip, password_list))
    cracked_password = crack_password(hash_value, password_list)

    # Display the result
    if cracked_password:
        result_label.config(text="Password is: {}".format(cracked_password))
    else:
        result_label.config(text="Password not found. The password entered was {}".format(password))
    
    # Display the hashed password
    hashed_password_label.config(text="Hashed password: {}".format(hash_value))

# Create the main window
root = tk.Tk()
root.title("Password Checker")
root.geometry("800x400")
root.configure(bg='#f2f2f2')

# Create a label for password input
password_label = tk.Label(root, text="Enter Password:", font=("Helvetica", 14), bg='#f2f2f2')

# Create an entry field for password input
password_entry = tk.Entry(root, show='*', font=("Helvetica", 14), width=25)

# Create a button to check the password
check_button = tk.Button(root, text="Check Password", font=("Helvetica", 14), command=check_password, bg ='#65f50c', fg='#ffffff', activebackground='#3e8e41', activeforeground='#ffffff', bd=0, padx=20, pady=10, highlightthickness=0)
check_button.config(borderwidth=1, relief="solid", highlightbackground='#ffffff', highlightcolor='#ffffff')
#not very important button
check_button.config(bg='#4CAF50', fg='black')

# Create a label to display the result
result_label = tk.Label(root, text="", font=("Helvetica", 14), bg='#f2f2f2', fg='#008080')

# Create a label to display the hashed password
hashed_password_label = tk.Label(root, text="", font=("Helvetica", 14), bg='#f2f2f2', fg='#008080')

# Pack the widgets into the window
password_label.pack(pady=10)
password_entry.pack(pady=5)
check_button.pack(pady=15)
result_label.pack(pady=10)
hashed_password_label.pack(pady=5)

# Start the main event loop
root.mainloop()
toiithl6

toiithl61#

添加result_label.configure
代码:

# Display the result
    if cracked_password:
        result_label.config(text="Password is: {}".format(cracked_password))
        result_label.configure(bg='yellow')
    else:
        result_label.config(text="Password not found. The password entered was {}".format(password))
        result_label.configure(bg='blue')

截图:

相关问题