📜 ⬆️ ⬇️

We write a simple counter of unique words in Python with a GUI on Tkinter

This article is written for those who have just started learning Python. In it, I will step by step describe the creation of a simple word counter from txt files using Tkinter. The source code is written under Python 2.7, at the end of the article I will add a few comments on how to transfer it under 3.6.

Where do we start?


There will be no extraordinary stairs in the program, so I recommend writing to IDLE; Of course, you can write without problems in PyCharm and in Eclipse.

Time to create the first and last function, which, in fact, will perform all calculations:

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() #  ,    —  

So what does this function do? Everything is obvious: it opens the file, reads the contents, removes any characters and creates a list from the contents of the file. Do not forget to remove the upper case by the lower method for a correct further check for the repetition of words. Further we will process this list. In fact, considering the length of this list len ​​function, we will already know the number of words.
')

Number of unique words


To count the number of unique words, create another list, which will be through a cycle to put the words that were not yet in it:

 nonrep_words = list() for word in words: if word in nonrep_words: #, "      ?" pass # ,     else: nonrep_words.append(word) # ,   

Further we will simply read the length of the nonrep_words list.

Graphical shell


First we need to connect the appropriate modules to our program:

 import Tkinter, Tkconstants, tkFileDialog from Tkinter import * 

Now after defining our main function, we will write a code that will display the main window, the name of our program, the “Import file” button and the output field:

 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() #    

In general, you can not use the frame for the button, label and text field, but if you want to add a few more functions in your future, then the frame will be useful for positioning.
When creating a button, we zabindili it on the function counter.

File Import and Output


At the beginning of the definition of the counter function, add the lines:

 output.delete("0.0","end") filename = tkFileDialog.askopenfilename() 

Everything is simple here: clean the output field with the delete method, then open the file with the askopenfilename method and pass its name to the filename variable.

Next, add to the end of the function:

 output.insert("end","Amount of words: %d\n" % len(words)) output.insert("end","Amount of nonrepeatable words: %d\n" % len(nonrep_words)) 

Here we print the length of two lists: a list of all words and a list of unique words.

All program code:

 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() 

For version 3.6


For this version of python the following changes should be made:

Source: https://habr.com/ru/post/340634/


All Articles