def counter(): with open(filename) as file: #   , filename   text = file.read() #  text = text.replace("\n", " ") text = text.replace(",", "").replace(".", "").replace("?", "").replace("!", "").replace("—", "") text = text.lower() #   words = text.split() #  ,    —   nonrep_words = list() for word in words: if word in nonrep_words: #, "      ?" pass # ,     else: nonrep_words.append(word) # ,    import Tkinter, Tkconstants, tkFileDialog from Tkinter import *  root = Tk() #  frame = Frame(root) #    frame.grid() #  grid title = Label(frame, text="Word counter") #  title.grid(row=1, column=1) # grid',    ,   row  column import_btn = Button(frame, text="Import file...", command=counter) #  import_btn.grid(row=2, column=1, pady=4) output = Text(frame, width=45, height=3) #   output.grid(row=4, columnspan=3) root.mainloop() #     output.delete("0.0","end") filename = tkFileDialog.askopenfilename()  output.insert("end","Amount of words: %d\n" % len(words)) output.insert("end","Amount of nonrepeatable words: %d\n" % len(nonrep_words))  import Tkinter, Tkconstants, tkFileDialog from Tkinter import * def counter(): output.delete("0.0","end") filename = tkFileDialog.askopenfilename() with open(filename) as file: text = file.read() text = text.replace("\n", " ") text = text.replace(",", "").replace(".", "").replace("?", "").replace("!", "").replace("—", "") text = text.lower() words = text.split() nonrep_words = list() for word in words: if word in nonrep_words: pass else: nonrep_words.append(word) output.insert("end","Amount of words: %d\n" % len(words)) output.insert("end","Amount of nonrepeatable words: %d\n" % len(nonrep_words)) root = Tk() frame = Frame(root) frame.grid() title = Label(frame, text="Word counter") title.grid(row=1, column=1) import_btn = Button(frame, text="Import file...", command=counter) import_btn.grid(row=2, column=1, pady=4) output = Text(frame, width=45, height=3) output.grid(row=4, columnspan=3) root.mainloop() tkinter write with a small letter; tkFileDialog remove and write instead of it from tkinter import filedialogtkFileDialog.askopenfilename file, tkFileDialog.askopenfilename replace it with filedialog.askopenfilenameencoding='utf-8' when opening a file through the managerSource: https://habr.com/ru/post/340634/
All Articles