Advertisement
dimkiriakos

read text file

Jan 22nd, 2022
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1.  
  2. import tkinter as tk
  3. from tkinter import filedialog as fd
  4. import pathlib
  5. def openFile():
  6.     filetypes = (
  7.         ('text files', '*.txt'),
  8.         ('All files', '*.*')
  9.     )
  10.     initDir = pathlib.Path().resolve()
  11.     try:
  12.         filename = fd.askopenfilename(
  13.             title='Επιλογή αρχείου',
  14.             initialdir=initDir,
  15.             filetypes=filetypes
  16.         )
  17.         f = open(filename, 'r')
  18.         content_list =  f.readlines()
  19.         f.close()
  20.        
  21.         contnet = ''
  22.         for c in content_list:
  23.             contnet += c
  24.         text_area.delete(1.0,"end")
  25.         text_area.insert(1.0, contnet)
  26.     except Exception as ex:
  27.         print(ex)
  28.  
  29.  
  30. root = tk.Tk()
  31. root.title('Κείμενα')
  32. root.geometry('500x300')
  33.  
  34.  
  35. info_label = tk.Label(text='Αναγνωση / Αποθήκευση αρχείου κειμένου')
  36. info_label.pack()
  37. text_area = tk.Text()
  38. text_area.pack()
  39. openButton = tk.Button(root,text='Άνοιγμα', command=openFile)
  40. openButton.pack()
  41.  
  42.  
  43. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement