import tkinter as tk from PIL import Image, ImageTk class UserFrame(tk.Frame): def __init__(self, master, user_data, current_logged_user, click_callback, *args, **kwargs): super().__init__(master, *args, **kwargs) self.current_logged_user = current_logged_user print(user_data) self.user_data = { 'id': user_data[0], 'username': user_data[1], 'email': user_data[3] } self.click_callback = click_callback # Create widgets self.create_widgets() def create_widgets(self): user_frame = tk.Frame(self, bg='white') # Set a background color for the user frame # Load the photo (replace "path/to/photo.png" with the actual path) photo_path = "/home/andrei/Documents/messenger/messenger_gui/resources/9131529.png" user_photo = self.load_image_with_alpha(photo_path, (50, 50)) # Create widgets for each user photo_label = tk.Label(user_frame, image=user_photo) div_frame = tk.Frame(user_frame, bg='white') # Set a background color for the div frame name_label = tk.Label(div_frame, text=self.user_data['username'], anchor="e", bg='white') # Align to the right email_label = tk.Label(div_frame, text=self.user_data['email'], anchor="e", bg='white') # Align to the right # Keep a reference to the PhotoImage to prevent it from being garbage collected photo_label.image = user_photo # Grid layout photo_label.grid(row=0, column=0, padx=5, pady=5) div_frame.grid(row=0, column=1, padx=5, pady=5) name_label.grid(row=0, column=0, padx=5, pady=5, sticky="e") email_label.grid(row=1, column=0, padx=5, pady=5, sticky="e") # Bind the click event to the user frame user_frame.bind("", lambda event, user_data=self.user_data, current_logged_user=self.current_logged_user: self.on_user_click(user_data, current_logged_user)) # Pack the user frame user_frame.pack(side=tk.LEFT, padx=10, pady=10) def on_user_click(self, user_data, current_logged_user): # Call the click_callback with the clicked user data if self.click_callback: self.click_callback(user_data) def load_image_with_alpha(self, path, size): image = Image.open(path).convert("RGBA").resize(size) image_without_alpha = Image.new("RGB", image.size, (255, 255, 255)) image_without_alpha.paste(image, mask=image.split()[3]) return ImageTk.PhotoImage(image_without_alpha)