Advertisement
Najeebsk

COLORHEX.pyw

Nov 24th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox
  3. from tkinter import ttk
  4.  
  5. def load_colors():
  6.     try:
  7.         with open("Color.txt", "r") as file:
  8.             color_data = []
  9.             for line in file:
  10.                 if line.strip():  # Ignore empty lines
  11.                     parts = line.strip().split("\t")
  12.                     if len(parts) >= 2:
  13.                         color_code = parts[0].strip()  # Ensure no extra spaces
  14.                         color_name = parts[1].strip()
  15.                         color_data.append((color_code, color_name))
  16.             return color_data
  17.     except FileNotFoundError:
  18.         messagebox.showerror("Error", "Color.txt file not found!")
  19.         return []
  20.  
  21. def show_color():
  22.     selected_index = listbox.curselection()
  23.     if selected_index:
  24.         color_code, color_name = colors[selected_index[0]]
  25.         apply_color(color_code, color_name)
  26.     else:
  27.         messagebox.showinfo("Info", "Please select a color from the list.")
  28.  
  29. def apply_color(color_code, color_name="Custom Color"):
  30.     try:
  31.         color_frame.config(bg=color_code)  # Update the color frame background
  32.         color_label.config(text=color_name)  # Update the label with color name
  33.         hex_label.config(text=f"Hex Color: {color_code}")  # Display hex color code
  34.     except tk.TclError:
  35.         messagebox.showerror("Error", f"Invalid color code: {color_code}")
  36.  
  37. def apply_custom_color():
  38.     color_code = color_entry.get().strip()
  39.     if color_code:
  40.         apply_color(color_code)
  41.     else:
  42.         messagebox.showinfo("Info", "Please enter a valid color code.")
  43.  
  44. # Create the main window
  45. root = tk.Tk()
  46. root.title("Najeeb Advanced Color Display")
  47. root.geometry("600x600")
  48. root.configure(bg="#f0f0f0")
  49.  
  50. # Load colors from file
  51. colors = load_colors()
  52.  
  53. # Create a main frame for layout
  54. main_frame = ttk.Frame(root, padding=10)
  55. main_frame.pack(fill=tk.BOTH, expand=True)
  56.  
  57. # Create a listbox to display the color descriptions
  58. listbox_frame = ttk.Frame(main_frame)
  59. listbox_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
  60.  
  61. listbox_label = ttk.Label(listbox_frame, text="Select a Color:", font=("Arial", 14))
  62. listbox_label.pack(anchor=tk.W, pady=5)
  63.  
  64. listbox = tk.Listbox(listbox_frame, selectmode=tk.SINGLE, font=("Arial", 12), height=10)
  65. for color_code, color_name in colors:
  66.     listbox.insert(tk.END, color_name)
  67. listbox.pack(fill=tk.BOTH, expand=True, side=tk.LEFT, padx=5)
  68.  
  69. # Add a scrollbar to the listbox
  70. scrollbar = ttk.Scrollbar(listbox_frame, orient=tk.VERTICAL, command=listbox.yview)
  71. listbox.config(yscrollcommand=scrollbar.set)
  72. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  73.  
  74. # Create a frame to display the selected color
  75. display_frame = ttk.Frame(main_frame)
  76. display_frame.pack(fill=tk.BOTH, pady=10)
  77.  
  78. color_frame = tk.Frame(display_frame, width=200, height=200, bg="white", relief=tk.RAISED, borderwidth=2)
  79. color_frame.grid(row=0, column=0, rowspan=2, padx=10)
  80.  
  81. color_label = ttk.Label(display_frame, text="No color selected", font=("Arial", 14))
  82. color_label.grid(row=0, column=1, sticky=tk.W, padx=10)
  83.  
  84. hex_label = ttk.Label(display_frame, text="Hex Color: #FFFFFF", font=("Arial", 12))
  85. hex_label.grid(row=1, column=1, sticky=tk.W, padx=10)
  86.  
  87. # Create an entry and button for custom color input
  88. custom_color_frame = ttk.Frame(main_frame)
  89. custom_color_frame.pack(fill=tk.X, pady=10)
  90.  
  91. color_entry_label = ttk.Label(custom_color_frame, text="Enter Color Code with # :", font=("Arial", 12))
  92. color_entry_label.pack(side=tk.LEFT, padx=5)
  93.  
  94. color_entry = ttk.Entry(custom_color_frame, font=("Arial", 12), width=15)
  95. color_entry.pack(side=tk.LEFT, padx=5)
  96.  
  97. apply_color_button = ttk.Button(custom_color_frame, text="Apply Color", command=apply_custom_color)
  98. apply_color_button.pack(side=tk.LEFT, padx=5)
  99.  
  100. # Create a button to show the selected color
  101. button_frame = ttk.Frame(main_frame)
  102. button_frame.pack(fill=tk.X, pady=10)
  103.  
  104. show_button = ttk.Button(button_frame, text="Show Color", command=show_color)
  105. show_button.pack(padx=5, pady=5)
  106.  
  107. # Run the main loop
  108. root.mainloop()
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement