Advertisement
xosski

Txt read/write

Dec 25th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. from os import listdir
  2. from tkinter import *
  3. from tkinter.scrolledtext import ScrolledText
  4.  
  5. current_file = "test.txt"
  6. files = listdir()
  7.  
  8. def on_select(event):
  9. """Handles the file selection from the listbox."""
  10. selected_index = files_list.curselection() # Get the index of selected item
  11. if selected_index:
  12. selected_item = files_list.get(selected_index)
  13. file_entry.delete(0, END)
  14. file_entry.insert(1, selected_item[:-4]) # Remove '.txt' extension for display
  15.  
  16. def save_file():
  17. """Saves the content of the text field into the current file."""
  18. with open(current_file, "w+", encoding="utf-8") as f:
  19. f.write(text_field.get(1.0, END))
  20.  
  21. def reset_file():
  22. """Clears the text field."""
  23. text_field.delete(1.0, END)
  24.  
  25. def get_file():
  26. """Loads the content of the current file into the text field."""
  27. try:
  28. with open(current_file, "r+", encoding="utf-8") as f:
  29. text_field.insert(1.0, f.read())
  30. except FileNotFoundError:
  31. print(f"File {current_file} not found.")
  32. reset_file()
  33.  
  34. def open_file():
  35. """Opens the file entered by the user in the entry field."""
  36. global current_file
  37. new_file = file_entry.get().strip()
  38.  
  39. if not new_file:
  40. print("No file name provided.")
  41. return
  42.  
  43. current_file = new_file + ".txt" # Open file with .txt extension
  44.  
  45. try:
  46. with open(current_file, "r+", encoding="utf-8") as f:
  47. reset_file()
  48. text_field.insert(1.0, f.read())
  49. print(f"Текущий файл: {current_file}")
  50. except FileNotFoundError:
  51. print(f"File {current_file} not found.")
  52. reset_file()
  53.  
  54. root = Tk() # Create main window
  55.  
  56. # Configure window parameters
  57. root['bg'] = "#fff9ed"
  58. root.geometry('800x600')
  59. root.title("Окно Tkinter")
  60.  
  61. # Text field with scroll functionality
  62. text_field = ScrolledText(root, wrap=WORD)
  63. text_field.place(relx=0.5, rely=0.33, anchor=CENTER)
  64.  
  65. # List of files in the current directory
  66. files_list = Listbox(root)
  67. for file in files:
  68. if file.endswith(".txt"): # Only include .txt files
  69. files_list.insert(END, file)
  70.  
  71. # Bind event to handle file selection
  72. files_list.bind('<<ListboxSelect>>', on_select)
  73.  
  74. # Buttons for actions
  75. save_button = Button(root, text="Сохранить", command=save_file)
  76. reset_button = Button(root, text="Сброс", command=reset_file)
  77. open_button = Button(root, text="Открыть файл", command=open_file)
  78. file_entry = Entry(root)
  79.  
  80. # Place buttons and entry field
  81. save_button.place(relx=0.15, rely=0.7)
  82. reset_button.place(relx=0.25, rely=0.7)
  83. open_button.place(relx=0.45, rely=0.7)
  84. file_entry.place(relx=0.60, rely=0.7)
  85. files_list.place(relx=0.60, rely=0.75)
  86.  
  87. # Initialize file reading (default file loading)
  88. get_file()
  89.  
  90. # Start the Tkinter main loop
  91. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement