text_editor.py 769 B

1234567891011121314151617181920212223242526
  1. from tkinter import *
  2. from tkinter import filedialog
  3. root = Tk()
  4. root.geometry('400x300')
  5. root.title('Текстовый редактор')
  6. root.resizable(False, False)
  7. def file_open():
  8. url = filedialog.askopenfilename()
  9. f = open(url)
  10. text.delete(0.0, END)
  11. text.insert(0.0, f.read())
  12. f.close()
  13. def file_save():
  14. url = filedialog.asksaveasfilename()
  15. f = open(url, 'w')
  16. f.write(text.get(0.0, END))
  17. f.close()
  18. rootmenu = Menu(root)
  19. root.config(menu = rootmenu)
  20. fm = Menu(rootmenu, tearoff = 0)
  21. fm.add_command(label = 'Открыть', command = file_open)
  22. fm.add_command(label = 'Сохранить', command = file_save)
  23. rootmenu.add_cascade(label = 'Файл', menu = fm)
  24. text = Text(width = 300, height = 400)
  25. text.pack()
  26. root.mainloop()