Untitled

mail@pastecode.io avatar
unknown
plain_text
9 days ago
6.9 kB
3
Indexable
Never
import tkinter as tk
from tkinter import ttk, filedialog, Text, messagebox
from PIL import Image, ImageTk
import pickle
import os

class NoteBook:
    def __init__(self, root):
        self.root = root
        self.root.title("Sic Monsters community")

        self.notebook = ttk.Notebook(root)
        self.notebook.pack(expand=1, fill='both')

        tab_names = ["Home Page", "Profile", "Games & Challenges", "Learn & Explore", "Search", "Setting"]

        for name in tab_names:
            frame = ttk.Frame(self.notebook)
            self.notebook.add(frame, text=name)

        self.create_home_page()
        self.load_posts()

    def create_home_page(self):
        home_frame = self.notebook.nametowidget(self.notebook.tabs()[0])

        self.post_frame = ttk.Frame(home_frame)
        self.post_frame.pack(padx=10, pady=10, fill='x')

        self.text_area = Text(self.post_frame, wrap='word', height=5)
        self.text_area.pack(padx=10, pady=5, fill='x')

        self.add_image_button = ttk.Button(self.post_frame, text="Add Image", command=self.add_image_post)
        self.add_image_button.pack(side='left', padx=5, pady=5)

        self.add_video_button = ttk.Button(self.post_frame, text="Add Video", command=self.add_video_post)
        self.add_video_button.pack(side='left', padx=5, pady=5)

        self.post_button = ttk.Button(self.post_frame, text="Post", command=self.add_post)
        self.post_button.pack(side='right', padx=5, pady=5)

        self.posts_container = ttk.Frame(home_frame)
        self.posts_container.pack(padx=10, pady=10, fill='both', expand=True)

    def add_post(self):
        post_content = self.text_area.get("1.0", tk.END).strip()
        if post_content:
            post_data = {"type": "text", "content": post_content, "comments": []}
            self.save_post(post_data)
            self.display_post(post_data)
            self.text_area.delete("1.0", tk.END)
        else:
            messagebox.showwarning("Empty Post", "Cannot add an empty post!")

    def add_image_post(self):
        file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.jpeg *.png *.gif")])
        if file_path:
            post_data = {"type": "image", "content": file_path, "comments": []}
            self.save_post(post_data)
            self.display_post(post_data)

    def add_video_post(self):
        file_path = filedialog.askopenfilename(filetypes=[("Video files", "*.mp4 *.avi *.mov")])
        if file_path:
            post_data = {"type": "video", "content": file_path, "comments": []}
            self.save_post(post_data)
            self.display_post(post_data)

    def save_post(self, post_data):
        if os.path.exists("posts.pkl"):
            with open("posts.pkl", "rb") as file:
                posts = pickle.load(file)
        else:
            posts = []

        posts.insert(0, post_data)

        with open("posts.pkl", "wb") as file:
            pickle.dump(posts, file)

    def load_posts(self):
        if os.path.exists("posts.pkl"):
            with open("posts.pkl", "rb") as file:
                posts = pickle.load(file)
                for post in posts:
                    self.display_post(post)

    def display_post(self, post_data):
        post_frame = ttk.Frame(self.posts_container, relief="solid", borderwidth=1)
        post_frame.pack(padx=10, pady=5, fill='x', anchor='w')

        if post_data["type"] == "text":
            post_label = ttk.Label(post_frame, text=post_data["content"], wraplength=400)
            post_label.pack(side='top', padx=5, pady=5)
        elif post_data["type"] == "image":
            image = Image.open(post_data["content"])
            image = image.resize((400, 300), Image.ANTIALIAS)
            image = ImageTk.PhotoImage(image)
            image_label = ttk.Label(post_frame, image=image)
            image_label.image = image  # Keep a reference to avoid garbage collection
            image_label.pack(side='top', padx=5, pady=5)
        elif post_data["type"] == "video":
            video_label = ttk.Label(post_frame, text=f"Video: {post_data['content']}")
            video_label.pack(side='top', padx=5, pady=5)

        like_button = ttk.Button(post_frame, text="Like", command=lambda: self.like_post(post_frame))
        like_button.pack(side='left', padx=5, pady=5)

        comment_button = ttk.Button(post_frame, text="Comment", command=lambda: self.comment_post(post_frame, post_data))
        comment_button.pack(side='left', padx=5, pady=5)

        delete_button = ttk.Button(post_frame, text="Delete", command=lambda: self.delete_post(post_frame, post_data))
        delete_button.pack(side='left', padx=5, pady=5)

        self.display_comments(post_frame, post_data)

    def display_comments(self, post_frame, post_data):
        for comment in post_data["comments"]:
            comment_label = ttk.Label(post_frame, text=comment, wraplength=400)
            comment_label.pack(side='top', padx=5, pady=5, anchor='w')

    def like_post(self, post_frame):
        messagebox.showinfo("Like", "You liked this post!")

    def comment_post(self, post_frame, post_data):
        comment_window = tk.Toplevel(self.root)
        comment_window.title("Add Comment")
        comment_text = Text(comment_window, wrap='word', height=5)
        comment_text.pack(padx=10, pady=10, fill='x')
        submit_button = ttk.Button(comment_window, text="Submit", command=lambda: self.submit_comment(comment_text, comment_window, post_frame, post_data))
        submit_button.pack(padx=10, pady=10)

    def submit_comment(self, comment_text, comment_window, post_frame, post_data):
        comment_content = comment_text.get("1.0", tk.END).strip()
        if comment_content:
            post_data["comments"].append(comment_content)
            self.save_post_data()
            self.display_comments(post_frame, post_data)
            comment_window.destroy()
        else:
            messagebox.showwarning("Empty Comment", "Cannot add an empty comment!")

    def delete_post(self, post_frame, post_data):
        if os.path.exists("posts.pkl"):
            with open("posts.pkl", "rb") as file:
                posts = pickle.load(file)
            posts.remove(post_data)
            with open("posts.pkl", "wb") as file:
                pickle.dump(posts, file)
        post_frame.destroy()

    def save_post_data(self):
        if os.path.exists("posts.pkl"):
            with open("posts.pkl", "rb") as file:
                posts = pickle.load(file)
        else:
            posts = []

        with open("posts.pkl", "wb") as file:
            pickle.dump(posts, file)


if __name__ == "__main__":
    root = tk.Tk()
    app = NoteBook(root)
    root.mainloop()
Leave a Comment