Building an Art Gallery Program in Python |
As an art lover, you may have considered creating a program to manage your favorite art pieces and display them in a virtual art gallery. This program can help you keep track of the details of each piece, including the image, description, and price. In this article, we will go through the process of building an art gallery program using Python and several libraries, including Tkinter, Pillow, and Pandas.
Importing Necessary Libraries
Before we start building our program, we need to import the libraries that we will be using. Tkinter will be used for creating the GUI, Pillow for handling image processing, and Pandas for data management.
Creating the Art Gallery Class
Next, we create a class for the art gallery program and initialize the necessary variables, such as the list of art pieces, their images, descriptions, and prices. We will also define the main window and its features, such as buttons for adding, editing, and removing art pieces, and input fields for the art piece's information.
Adding, Editing, and Deleting Art Pieces
We write the logic for handling the add, edit, and remove operations for art pieces, which can be done using Pandas data frames. The add_art_piece function allows the user to input the details of a new art piece, such as the image, description, and price. The edit_art_piece function allows the user to change the details of an existing art piece, and the delete_art_piece function removes an art piece from the gallery.
Incorporating Image Processing and Displaying Detailed Information
Searching and Filtering Art Pieces
We add functionality for searching and filtering art pieces based on certain criteria, such as price range, artist, or genre. This will make it easier for the user to find a specific art piece or to view pieces that meet certain criteria.
Example Code:
import tkinter as tk | |
import pandas as pd | |
from PIL import Image, ImageTk | |
import tkinter.messagebox as messagebox | |
class ArtGallery: | |
def __init__(self, master): | |
self.master = master | |
self.master.title("Art Gallery") | |
self.master.geometry("600x400") | |
# Initialize dataframe to store art pieces | |
self.art_pieces = pd.DataFrame(columns=["Name", "Image", "Description", "Price"]) | |
# Create main window to display art pieces | |
self.main_frame = tk.Frame(self.master) | |
self.main_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) | |
# Create scrollbar for main window | |
self.scrollbar = tk.Scrollbar(self.main_frame) | |
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y) | |
# Create listbox to display art pieces | |
self.listbox = tk.Listbox(self.main_frame, yscrollcommand=self.scrollbar.set) | |
self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) | |
self.scrollbar.config(command=self.listbox.yview) | |
# Create right frame to display detailed information | |
self.right_frame = tk.Frame(self.master) | |
self.right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True) | |
# Create label to display image | |
self.image_label = tk.Label(self.right_frame) | |
self.image_label.pack() | |
# Create label to display description | |
self.description_label = tk.Label(self.right_frame, text="Description:") | |
self.description_label.pack() | |
# Create text widget to display detailed description | |
self.description_text = tk.Text(self.right_frame) | |
self.description_text.pack() | |
# Create label to display price | |
self.price_label = tk.Label(self.right_frame, text="Price:") | |
self.price_label.pack() | |
# Create label to display detailed price | |
self.price_value_label = tk.Label(self.right_frame) | |
self.price_value_label.pack() | |
# Create add button | |
self.add_button = tk.Button(self.right_frame, text="Add", command=self.add_art_piece) | |
self.add_button.pack() | |
# Create edit button | |
self.edit_button = tk.Button(self.right_frame, text="Edit", command=self.edit_art_piece) | |
self.edit_button.pack() | |
# Create remove button | |
self.remove_button = tk.Button(self.right_frame, text="Remove", command=self.remove_art_piece) | |
self.remove_button.pack() | |
def add_art_piece(self): | |
# Get the user input for the art piece's information | |
title = self.title_entry.get() | |
artist = self.artist_entry.get() | |
description = self.description_entry.get() | |
price = self.price_entry.get() | |
image_path = self.image_entry.get() | |
# Validate the user input | |
if not title or not artist or not description or not price or not image_path: | |
messagebox.showerror("Error", "All fields are required.") | |
return | |
if not price.isdigit(): | |
messagebox.showerror("Error", "Price must be a number.") | |
return | |
# Add the art piece information to the dataframe | |
self.df = self.df.append({"Title": title, "Artist": artist, "Description": description, "Price": int(price), "Image": image_path}, ignore_index=True) | |
# Refresh the main window to display the new art piece | |
self.refresh_window() | |
def edit_art_piece(self): | |
# Get the selected art piece's index | |
selected_index = self.listbox.curselection() | |
# Check if an art piece is selected | |
if selected_index: | |
# Get the selected art piece's information | |
selected_art_piece = self.df.loc[selected_index[0]] | |
# Open a new window for editing the art piece's information | |
edit_window = tk.Toplevel(self.root) | |
edit_window.title("Edit Art Piece") | |
# Create the input fields for the art piece's information | |
tk.Label(edit_window, text="Name:").grid(row=0, column=0, sticky="W") | |
name_entry = tk.Entry(edit_window) | |
name_entry.insert(0, selected_art_piece["Name"]) | |
name_entry.grid(row=0, column=1) | |
tk.Label(edit_window, text="Image:").grid(row=1, column=0, sticky="W") | |
image_entry = tk.Entry(edit_window) | |
image_entry.insert(0, selected_art_piece["Image"]) | |
image_entry.grid(row=1, column=1) | |
tk.Label(edit_window, text="Description:").grid(row=2, column=0, sticky="W") | |
description_entry = tk.Entry(edit_window) | |
description_entry.insert(0, selected_art_piece["Description"]) | |
description_entry.grid(row=2, column=1) | |
tk.Label(edit_window, text="Price:").grid(row=3, column=0, sticky="W") | |
price_entry = tk.Entry(edit_window) | |
price_entry.insert(0, selected_art_piece["Price"]) | |
price_entry.grid(row=3, column=1) | |
tk.Label(edit_window, text="Artist:").grid(row=4, column=0, sticky="W") | |
artist_entry = tk.Entry(edit_window) | |
artist_entry.insert(0, selected_art_piece["Artist"]) | |
artist_entry.grid(row=4, column=1) | |
# Create the save button | |
save_button = tk.Button(edit_window, text="Save", command=lambda: self.save_edit(selected_index[0], name_entry.get(), image_entry.get(), description_entry.get(), price_entry.get(), artist_entry.get())) | |
save_button.grid(row=5, column=0, columnspan=2, pady=10) | |
else: | |
# If no art piece is selected, show an error message | |
tk.messagebox.showerror("Error", "Please select an art piece to edit.") | |
def delete_art_piece(self): | |
# Get the selected art piece to delete | |
selected_art = self.listbox.get(self.listbox.curselection()) | |
# Confirm the deletion with the user | |
confirm = messagebox.askyesno("Delete Confirmation", "Are you sure you want to delete " + selected_art + "?") | |
if confirm: | |
# Delete the selected art piece from the dataframe | |
self.df = self.df[self.df.title != selected_art] | |
# Update the listbox to reflect the deletion | |
self.update_listbox() | |
messagebox.showinfo("Success", selected_art + " was deleted successfully.") | |
def display_art_info(art_piece): | |
# Create a new window to display the detailed information | |
info_window = tk.Toplevel(root) | |
info_window.title("Art Piece Information") | |
# Display the image of the art piece | |
image = Image.open(art_piece["image"]) | |
image = image.resize((200, 200), Image.ANTIALIAS) | |
image = ImageTk.PhotoImage(image) | |
image_label = tk.Label(info_window, image=image) | |
image_label.image = image | |
image_label.pack() | |
# Display the description and price of the art piece | |
description = tk.Label(info_window, text=art_piece["description"]) | |
description.pack() | |
price = tk.Label(info_window, text="Price: ${}".format(art_piece["price"])) | |
price.pack() | |
def search_art_pieces(self): | |
search_text = self.search_entry.get() | |
filtered_df = self.df[ | |
(self.df['Artist'].str.contains(search_text, case=False)) | | |
(self.df['Genre'].str.contains(search_text, case=False)) | | |
(self.df['Description'].str.contains(search_text, case=False)) | |
] | |
self.display_art_pieces(filtered_df) | |
def filter_art_pieces(self): | |
selected_artist = self.artist_var.get() | |
selected_genre = self.genre_var.get() | |
filtered_df = self.df[ | |
(self.df['Artist'] == selected_artist) | | |
(self.df['Genre'] == selected_genre) | |
] | |
self.display_art_pieces(filtered_df) |
Note: This is not a complete source code but a starting point, if you need guidance, please buy our books to learn more about these.
Final Thoughts
With these functions, we have created a fully functional art gallery program in Python using Tkinter for the GUI, Pillow for image processing, and Pandas for data management. The program allows the user to view all the art pieces, add new pieces, edit existing pieces, and delete pieces. Additionally, the user can search for pieces based on certain criteria and filter the pieces by artist and genre. Finally, error handling has been added to ensure that the program is robust and user-friendly.
Food for Thought
- Consider adding more features to the program, such as the ability to export and import the art gallery data.
- Think about how you could improve the user experience, such as by adding animations or making the GUI more visually appealing.