Advertisement
JEXBLUK

file explorer (use file name.type in images)

May 30th, 2024 (edited)
868
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | Source Code | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog
  3. import pygame
  4. from PIL import Image, ImageTk
  5.  
  6. class FileExplorer:
  7.     def __init__(self, master):
  8.         self.master = master
  9.         self.master.title("File Explorer")
  10.  
  11.         self.file_label = tk.Label(master, text="Selected File:")
  12.         self.file_label.pack()
  13.  
  14.         self.select_button = tk.Button(master, text="Select File", command=self.select_file)
  15.         self.select_button.pack()
  16.  
  17.         self.read_button = tk.Button(master, text="Read", command=self.read_file, state=tk.DISABLED)
  18.         self.read_button.pack()
  19.  
  20.         self.play_button = tk.Button(master, text="Play", command=self.play, state=tk.DISABLED)
  21.         self.play_button.pack()
  22.  
  23.         self.stop_button = tk.Button(master, text="Stop", command=self.stop, state=tk.DISABLED)
  24.         self.stop_button.pack()
  25.  
  26.         self.image_button = tk.Button(master, text="View Image", command=self.view_image, state=tk.DISABLED)
  27.         self.image_button.pack()
  28.  
  29.         self.image_label = tk.Label(master)
  30.         self.image_label.pack()
  31.  
  32.     def select_file(self):
  33.         filename = filedialog.askopenfilename(filetypes=[("MP3 files", "*.mp3"), ("Text files", "*.txt"), ("Image files", "*.jpg;*.jpeg;*.png;*.gif")])
  34.         if filename:
  35.             self.file_label.config(text=f"Selected File: {filename}")
  36.             if filename.endswith(".txt"):
  37.                 self.read_button.config(state=tk.NORMAL)
  38.                 self.play_button.config(state=tk.DISABLED)
  39.                 self.stop_button.config(state=tk.DISABLED)
  40.                 self.image_button.config(state=tk.DISABLED)
  41.                 self.image_label.config(image=None)
  42.             elif filename.endswith((".jpg", ".jpeg", ".png", ".gif")):
  43.                 self.read_button.config(state=tk.DISABLED)
  44.                 self.play_button.config(state=tk.DISABLED)
  45.                 self.stop_button.config(state=tk.DISABLED)
  46.                 self.image_button.config(state=tk.NORMAL)
  47.                 self.update_image(filename)  # Update the displayed image
  48.             else:
  49.                 self.read_button.config(state=tk.DISABLED)
  50.                 self.play_button.config(state=tk.NORMAL)
  51.                 self.stop_button.config(state=tk.DISABLED)
  52.                 self.image_button.config(state=tk.DISABLED)
  53.                 self.image_label.config(image=None)
  54.             self.filename = filename
  55.  
  56.     def read_file(self):
  57.         with open(self.filename, "r") as file:
  58.             content = file.read()
  59.         tk.messagebox.showinfo("File Content", content)
  60.  
  61.     def play(self):
  62.         pygame.mixer.init()
  63.         pygame.mixer.music.load(self.filename)
  64.         pygame.mixer.music.play()
  65.         self.play_button.config(state=tk.DISABLED)
  66.         self.stop_button.config(state=tk.NORMAL)
  67.  
  68.     def stop(self):
  69.         pygame.mixer.music.stop()
  70.         self.play_button.config(state=tk.NORMAL)
  71.         self.stop_button.config(state=tk.DISABLED)
  72.  
  73.     def update_image(self, filename):
  74.         try:
  75.             image = Image.open(filename)
  76.             self.image = ImageTk.PhotoImage(image)
  77.         except Exception as e:
  78.             tk.messagebox.showerror("Error", str(e))
  79.  
  80.     def view_image(self):
  81.         try:
  82.             self.image_label.config(image=self.image)
  83.         except AttributeError:
  84.             tk.messagebox.showerror("Error", "No image selected.")
  85.         except Exception as e:
  86.             tk.messagebox.showerror("Error", str(e))
  87.  
  88. def main():
  89.     root = tk.Tk()
  90.     app = FileExplorer(root)
  91.     root.mainloop()
  92.  
  93. if __name__ == "__main__":
  94.     main()
Advertisement
Comments
  • JEXBLUK
    205 days (edited)
    # text 0.10 KB | 0 0
    1. contact me on discord
    2. skilltsl
    3. or search me in YT SKILL TSL
    4. make sure install
    5. Tkinder
    6. pygame
    7. pillow
Add Comment
Please, Sign In to add comment
Advertisement