Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox
- from tkinter import ttk
- def load_colors():
- try:
- with open("Color.txt", "r") as file:
- color_data = []
- for line in file:
- if line.strip(): # Ignore empty lines
- parts = line.strip().split("\t")
- if len(parts) >= 2:
- color_code = parts[0].strip() # Ensure no extra spaces
- color_name = parts[1].strip()
- color_data.append((color_code, color_name))
- return color_data
- except FileNotFoundError:
- messagebox.showerror("Error", "Color.txt file not found!")
- return []
- def show_color():
- selected_index = listbox.curselection()
- if selected_index:
- color_code, color_name = colors[selected_index[0]]
- apply_color(color_code, color_name)
- else:
- messagebox.showinfo("Info", "Please select a color from the list.")
- def apply_color(color_code, color_name="Custom Color"):
- try:
- color_frame.config(bg=color_code) # Update the color frame background
- color_label.config(text=color_name) # Update the label with color name
- hex_label.config(text=f"Hex Color: {color_code}") # Display hex color code
- except tk.TclError:
- messagebox.showerror("Error", f"Invalid color code: {color_code}")
- def apply_custom_color():
- color_code = color_entry.get().strip()
- if color_code:
- apply_color(color_code)
- else:
- messagebox.showinfo("Info", "Please enter a valid color code.")
- # Create the main window
- root = tk.Tk()
- root.title("Najeeb Advanced Color Display")
- root.geometry("600x600")
- root.configure(bg="#f0f0f0")
- # Load colors from file
- colors = load_colors()
- # Create a main frame for layout
- main_frame = ttk.Frame(root, padding=10)
- main_frame.pack(fill=tk.BOTH, expand=True)
- # Create a listbox to display the color descriptions
- listbox_frame = ttk.Frame(main_frame)
- listbox_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
- listbox_label = ttk.Label(listbox_frame, text="Select a Color:", font=("Arial", 14))
- listbox_label.pack(anchor=tk.W, pady=5)
- listbox = tk.Listbox(listbox_frame, selectmode=tk.SINGLE, font=("Arial", 12), height=10)
- for color_code, color_name in colors:
- listbox.insert(tk.END, color_name)
- listbox.pack(fill=tk.BOTH, expand=True, side=tk.LEFT, padx=5)
- # Add a scrollbar to the listbox
- scrollbar = ttk.Scrollbar(listbox_frame, orient=tk.VERTICAL, command=listbox.yview)
- listbox.config(yscrollcommand=scrollbar.set)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- # Create a frame to display the selected color
- display_frame = ttk.Frame(main_frame)
- display_frame.pack(fill=tk.BOTH, pady=10)
- color_frame = tk.Frame(display_frame, width=200, height=200, bg="white", relief=tk.RAISED, borderwidth=2)
- color_frame.grid(row=0, column=0, rowspan=2, padx=10)
- color_label = ttk.Label(display_frame, text="No color selected", font=("Arial", 14))
- color_label.grid(row=0, column=1, sticky=tk.W, padx=10)
- hex_label = ttk.Label(display_frame, text="Hex Color: #FFFFFF", font=("Arial", 12))
- hex_label.grid(row=1, column=1, sticky=tk.W, padx=10)
- # Create an entry and button for custom color input
- custom_color_frame = ttk.Frame(main_frame)
- custom_color_frame.pack(fill=tk.X, pady=10)
- color_entry_label = ttk.Label(custom_color_frame, text="Enter Color Code with # :", font=("Arial", 12))
- color_entry_label.pack(side=tk.LEFT, padx=5)
- color_entry = ttk.Entry(custom_color_frame, font=("Arial", 12), width=15)
- color_entry.pack(side=tk.LEFT, padx=5)
- apply_color_button = ttk.Button(custom_color_frame, text="Apply Color", command=apply_custom_color)
- apply_color_button.pack(side=tk.LEFT, padx=5)
- # Create a button to show the selected color
- button_frame = ttk.Frame(main_frame)
- button_frame.pack(fill=tk.X, pady=10)
- show_button = ttk.Button(button_frame, text="Show Color", command=show_color)
- show_button.pack(padx=5, pady=5)
- # Run the main loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement