Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog
- import pygame
- from PIL import Image, ImageTk
- class FileExplorer:
- def __init__(self, master):
- self.master = master
- self.master.title("File Explorer")
- self.file_label = tk.Label(master, text="Selected File:")
- self.file_label.pack()
- self.select_button = tk.Button(master, text="Select File", command=self.select_file)
- self.select_button.pack()
- self.read_button = tk.Button(master, text="Read", command=self.read_file, state=tk.DISABLED)
- self.read_button.pack()
- self.play_button = tk.Button(master, text="Play", command=self.play, state=tk.DISABLED)
- self.play_button.pack()
- self.stop_button = tk.Button(master, text="Stop", command=self.stop, state=tk.DISABLED)
- self.stop_button.pack()
- self.image_button = tk.Button(master, text="View Image", command=self.view_image, state=tk.DISABLED)
- self.image_button.pack()
- self.image_label = tk.Label(master)
- self.image_label.pack()
- def select_file(self):
- filename = filedialog.askopenfilename(filetypes=[("MP3 files", "*.mp3"), ("Text files", "*.txt"), ("Image files", "*.jpg;*.jpeg;*.png;*.gif")])
- if filename:
- self.file_label.config(text=f"Selected File: {filename}")
- if filename.endswith(".txt"):
- self.read_button.config(state=tk.NORMAL)
- self.play_button.config(state=tk.DISABLED)
- self.stop_button.config(state=tk.DISABLED)
- self.image_button.config(state=tk.DISABLED)
- self.image_label.config(image=None)
- elif filename.endswith((".jpg", ".jpeg", ".png", ".gif")):
- self.read_button.config(state=tk.DISABLED)
- self.play_button.config(state=tk.DISABLED)
- self.stop_button.config(state=tk.DISABLED)
- self.image_button.config(state=tk.NORMAL)
- self.update_image(filename) # Update the displayed image
- else:
- self.read_button.config(state=tk.DISABLED)
- self.play_button.config(state=tk.NORMAL)
- self.stop_button.config(state=tk.DISABLED)
- self.image_button.config(state=tk.DISABLED)
- self.image_label.config(image=None)
- self.filename = filename
- def read_file(self):
- with open(self.filename, "r") as file:
- content = file.read()
- tk.messagebox.showinfo("File Content", content)
- def play(self):
- pygame.mixer.init()
- pygame.mixer.music.load(self.filename)
- pygame.mixer.music.play()
- self.play_button.config(state=tk.DISABLED)
- self.stop_button.config(state=tk.NORMAL)
- def stop(self):
- pygame.mixer.music.stop()
- self.play_button.config(state=tk.NORMAL)
- self.stop_button.config(state=tk.DISABLED)
- def update_image(self, filename):
- try:
- image = Image.open(filename)
- self.image = ImageTk.PhotoImage(image)
- except Exception as e:
- tk.messagebox.showerror("Error", str(e))
- def view_image(self):
- try:
- self.image_label.config(image=self.image)
- except AttributeError:
- tk.messagebox.showerror("Error", "No image selected.")
- except Exception as e:
- tk.messagebox.showerror("Error", str(e))
- def main():
- root = tk.Tk()
- app = FileExplorer(root)
- root.mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Comments
-
- contact me on discord
- skilltsl
- or search me in YT SKILL TSL
- make sure install
- Tkinder
- pygame
- pillow
Add Comment
Please, Sign In to add comment
Advertisement