TkinterのGUIを使用して、Pythonで一意の単語の簡単なカウンターを作成します

この記事は、Pythonを学び始めたばかりの人向けに書かれています。 その中で、Tkinterを使用してtxtファイルから単純なワードカウンターを作成する方法をステップごとに説明します。 ソースコードはPython 2.7で記述されています。記事の最後に、3.6での移植方法に関するコメントをいくつか追加します。



どこから始めますか?



プログラムには目立つラダーはないので、IDLEで書くことをお勧めします。 もちろん、問題なくPyCharmとEclipseで書くことができます。



実際にすべての計算を実行する最初と最後の関数を作成します。



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





それでは、この関数は何をするのでしょうか? すべてが明らかです。ファイルを開き、内容を読み取り、あらゆる種類の文字を削除し、ファイルの内容からリストを作成します。 単語の繰り返しをさらに正確に検証するために、小文字のメソッドで大文字を削除することを忘れないでください。 さらに、このリストを処理します。 実際、このリストの長さをlen関数と見なすと、単語の数がすでにわかります。



ユニークワードの数



一意の単語の数をカウントするには、別のリストを作成します。このリストでは、サイクルを経て、まだ含まれていない単語を入力します。



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





次に、nonrep_wordsリストの長さを単純に読み取ります。



グラフィカルシェル



まず、適切なモジュールをプログラムに接続する必要があります。



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





ここではすべてが簡単です。deleteメソッドで出力フィールドをクリアし、askopenfilenameメソッドでファイルを開いて、その名前をfilename変数に渡します。



次に、関数の最後に追加します。



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





ここでは、すべての単語のリストと一意の単語のリストの2つのリストの長さを表示します。



すべてのプログラムコード:



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





バージョン3.6の場合



このバージョンのPythonでは、次の変更を行う必要があります。






All Articles