Advertisement
xosski

File Loader/Slip with gui

Dec 25th, 2024
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import os
  2. from tkinter import *
  3. from tkinter.scrolledtext import ScrolledText
  4. from tkinter import messagebox
  5.  
  6. current_file = "test.txt"
  7. files = [f for f in os.listdir() if f.endswith(".txt")] # Only show .txt files
  8.  
  9. # Function to handle selection from the file list
  10. def on_select(event):
  11. selected_index = files_list.curselection() # Get index of the selected item
  12. if selected_index:
  13. selected_item = files_list.get(selected_index)
  14. file_entry.delete(0, END)
  15. file_entry.insert(0, selected_item[:-4]) # Set file name without extension
  16.  
  17. # Function to save content to the current file
  18. def save_file():
  19. try:
  20. with open(current_file, "w+", encoding="utf-8") as f:
  21. f.write(text_field.get(1.0, END).strip()) # Remove trailing newlines/extra spaces
  22. messagebox.showinfo("Success", f"File '{current_file}' saved successfully!")
  23. except Exception as e:
  24. messagebox.showerror("Error", f"Failed to save file: {e}")
  25.  
  26. # Function to reset text field
  27. def reset_file():
  28. text_field.delete(1.0, END)
  29.  
  30. # Function to load content from the current file
  31. def get_file():
  32. try:
  33. with open(current_file, "r+", encoding="utf-8") as f:
  34. text_field.insert(1.0, f.read())
  35. except FileNotFoundError:
  36. messagebox.showerror("Error", f"File '{current_file}' not found!")
  37.  
  38. # Function to open a new file
  39. def open_file():
  40. global current_file
  41. new_file = file_entry.get().strip()
  42.  
  43. if not new_file: # Ensure file name is not empty
  44. messagebox.showwarning("Input Error", "Please provide a valid file name.")
  45. return
  46.  
  47. # Add .txt extension and check if file exists
  48. current_file = new_file + ".txt"
  49. if current_file not in files:
  50. messagebox.showerror("File Not Found", f"The file '{current_file}' does not exist.")
  51. return
  52.  
  53. try:
  54. with open(current_file, "r+", encoding="utf-8") as f:
  55. reset_file()
  56. text_field.insert(1.0, f.read())
  57. messagebox.showinfo("Success", f"Opened file: {current_file}")
  58. except Exception as e:
  59. messagebox.showerror("Error", f"Failed to open file: {e}")
  60.  
  61. # Main application window
  62. root = Tk()
  63.  
  64. # Configure the root window
  65. root['bg'] = "#fff9ed"
  66. root.geometry('800x600')
  67. root.title("Text File Editor")
  68.  
  69. # Text field for editing
  70. text_field = ScrolledText(root, wrap=WORD, width=80, height=20)
  71. text_field.place(relx=0.5, rely=0.33, anchor=CENTER)
  72.  
  73. # Listbox for file selection
  74. files_list = Listbox(root)
  75. for file in files:
  76. files_list.insert(END, file)
  77. files_list.bind('<<ListboxSelect>>', on_select)
  78.  
  79. # Buttons for actions
  80. save_button = Button(root, text="Save", command=save_file)
  81. reset_button = Button(root, text="Reset", command=reset_file)
  82. open_button = Button(root, text="Open", command=open_file)
  83.  
  84. # File entry to show the selected file name
  85. file_entry = Entry(root)
  86.  
  87. # Place the widgets in the window
  88. save_button.place(relx=0.15, rely=0.7)
  89. reset_button.place(relx=0.25, rely=0.7)
  90. open_button.place(relx=0.45, rely=0.7)
  91. file_entry.place(relx=0.60, rely=0.7)
  92. files_list.place(relx=0.60, rely=0.75)
  93.  
  94. # Load the current file if it exists
  95. get_file()
  96.  
  97. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement