Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
3.7 kB
2
Indexable
Never
import tkinter as tk
from tkinter import PhotoImage
import json


class Stack:
    def __init__(self):
        self.posts = []

    def isempty(self):
        return self.posts == []

    def addpost(self, post):
        self.posts.append(post)

    def removepost(self):
        return None if self.isempty() else self.posts.pop()

    def showpost(self):
        return None if self.isempty() else self.posts[-1]


class GUI:
    def __init__(self, root):
        self.root = root
        self.root.title("Home Page")
        self.root.geometry("500x600")
        self.root.configure(bg="#3b5998")

        # Load profile image
        self.image = PhotoImage(file="C:/Users/ACER/Desktop/samsung innovation campus/avatar.png")
        label = tk.Label(root, text="Welcomeback", font=('arial', 20), image=self.image, compound='left')
        label.pack(pady=20, padx=20)

        self.post_stack = Stack()
        self.load_posts('posts.json')
        self.display_posts()

    def load_posts(self, filename):
        with open(filename, "r") as file:
            data = json.load(file)
            print(data)
            for post in data["posts"]:
                self.post_stack.addpost(post)

    def display_posts(self):
        while not self.post_stack.isempty():
            post = self.post_stack.removepost()
            post_label = tk.Label(self.root, text=f"Post: {post['caption']}", font=('Arial', 16), bg='#f0f0f0')
            post_label.pack(pady=10)
            likes_label = tk.Label(self.root, text=f"Likes: {post['likes']}", font=('Arial', 12), bg='#f0f0f0')
            likes_label.pack(pady=5)

            # Like button
            like_button = tk.Button(self.root, text="Like",
                                    command=lambda p=post, l=likes_label: self.like_post(p, l))
            like_button.pack(pady=5)

            # Comment section
            comment_label = tk.Label(self.root, text="Add a comment:", bg='#f0f0f0')
            comment_label.pack(pady=5)

            comment_entry = tk.Entry(self.root, width=40)
            comment_entry.pack(pady=5)

            comments_frame = tk.Frame(self.root, bg='#f0f0f0')
            comments_frame.pack(pady=5)

            if post.get('comments'):
                for comment in post['comments']:
                    comment_label = tk.Label(comments_frame, text=f"Comment: {comment['content']}", font=('Arial', 10),
                                             bg='#e0e0e0')
                    comment_label.pack(padx=20, pady=2)

            comment_button = tk.Button(self.root, text="Comment",
                                       command=lambda p=post, c=comment_entry: self.comment(p, c))
            comment_button.pack(pady=5)

    def like_post(self, post, likes_label):
        post['likes'] += 1
        likes_label.config(text=f"Likes: {post['likes']}")

    def comment(self, post, comment_entry):
        comments = comment_entry.get()
        if comments:
            comment_id = len(post['comments']) + 1
            newcomment = {
                "comment_id": comment_id,
                "content": comments
            }
            post["comments"].append(newcomment)
            self.saveposts()
            commentlabel = tk.Label(self.root,text=f"Comment: {newcomment['content']}", font=("Arial", 10),
                                    bg='#e0e0e0')
            commentlabel.pack(padx=20, pady=2)

    def saveposts(self):
        with open("posts.json","w") as file:
            json.dump({"posts":self.post_stack.posts},file,indent=2)


root = tk.Tk()
app = GUI(root)
root.mainloop()
Leave a Comment