SHARE
    TWEET
    Najeebsk

    COLORHEX.pyw

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

    AltStyle によって変換されたページ (->オリジナル) /