2010-12-12 7 views
1

私はPythonでGUIを使っていくつかの作業をしています。私はTkinterライブラリを使用しています。tkinterイベントに応答するには?

私は.txtファイルを開いて、処理のこのビットを行いますボタンを、必要とする:

frequencies = collections.defaultdict(int) # <----------------------- 
with open("test.txt") as f_in:     
    for line in f_in: 
     for char in line: 
      frequencies[char] += 1 
total = float(sum(frequencies.values()))  #<------------------------- 

私が開始しました:今、私は組み立てする方法がわからない

from Tkinter import *    
import tkFileDialog,Tkconstants,collections 

root = Tk() 
root.title("TEST") 
root.geometry("800x600") 

button_opt = {'fill': Tkconstants.BOTH, 'padx': 66, 'pady': 5} 
fileName = '' 
def openFile(): 
    fileName = tkFileDialog.askopenfile(parent=root,title="Open .txt file", filetypes=[("txt file",".txt"),("All files",".*")]) 
Button(root, text = 'Open .txt file', fg = 'black', command= openFile).pack(**button_opt) 



frequencies = collections.defaultdict(int) # <----------------------- 
with open("test.txt") as f_in:     
    for line in f_in: 
     for char in line: 
      frequencies[char] += 1 
total = float(sum(frequencies.values()))  #<------------------------- 



root.mainloop() 

私のコードので、ボタンが押されたときに実行されます。

答えて

2

主な問題はtkFileDialog.askopenfile()でした。ファイル名ではなくオープンfileを返します。これ以下は、多かれ少なかれ私のために働いているように見えた:

from Tkinter import * 
import tkFileDialog, Tkconstants,collections 

root = Tk() 
root.title("TEST") 
root.geometry("800x600") 

def openFile(): 
    f_in = tkFileDialog.askopenfile(
          parent=root, 
          title="Open .txt file", 
          filetypes=[("txt file",".txt"),("All files",".*")]) 

    frequencies = collections.defaultdict(int) 
    for line in f_in: 
     for char in line: 
      frequencies[char] += 1 
    f_in.close() 
    total = float(sum(frequencies.values())) 
    print 'total:', total 

button_opt = {'fill': Tkconstants.BOTH, 'padx': 66, 'pady': 5} 
fileName = '' 
Button(root, text = 'Open .txt file', 
     fg = 'black', 
     command= openFile).pack(**button_opt) 

root.mainloop() 

早く、私は非常にEasyGUIをお勧めします簡単なGUIプログラム、かなり強力かつシンプルなTkはそのようなことを行うためのPythonモジュールを--basedを作成します。

+0

多くのありがとう;)助けを求めて – thaking

+0

3分でビート... +1良い答え、あまりにも複雑ではない鉱山。 – John

0

openFile()の機能では、ユーザーにファイル名を尋ねた直後に、コードを入力してください。

+0

これはうまくいかない、私はこれを試したが、私がそれを実行すると、最初にウィンドウのダイアログを開いて、text.fileを開く。私は、ボタンをクリックし、それをクリックし、.txtファイルを開き、 "コード"に何をしてください – thaking

1

ビットのようにレイアウト何か試してみてください:あなたはまた、読むことをお勧めします

class my_app(): 
    def __init__(): 
     self.hi_there = Tkinter.Button(frame, text="Hello", command=self.say_hi) 
     self.hi_there.pack(side=Tkinter.LEFT) 

    def say_hi(): 
     # do stuff 

を:

This tutorial on Tkinter,

And this one.

EDIT:ザ・OPは例を望んでいました彼のコードで(私は思うので)ここにそれがあります:

from Tkinter import *    
import tkFileDialog,Tkconstants,collections 

class my_app: 
    def __init__(self, master): 
     frame = Tkinter.Frame(master) 
     frame.pack() 

     self.button_opt = {'fill': Tkconstants.BOTH, 'padx': 66, 'pady': 5} 
     self.button = Button(frame, text = 'Open .txt file', fg = 'black', command= self.openFile).pack(**button_opt) 

     self.calc_button = Button(frame, text = 'Calculate', fg = 'black', command= self.calculate).pack() 

     self.fileName = '' 

    def openFile(): 
     fileName = tkFileDialog.askopenfile(parent=root,title="Open .txt file", filetypes=[("txt file",".txt"),("All files",".*")]) 

    def calculate(): 
     ############################################### *See note 
     frequencies = collections.defaultdict(int) # <----------------------- 
     with open("test.txt") as f_in:     
      for line in f_in: 
       for char in line: 
        frequencies[char] += 1 
     total = float(sum(frequencies.values()))  #<------------------------- 
     ################################################ 

root = Tk() 

app = App(root) 

root.title("TEST") 
root.geometry("800x600") 

root.mainloop() 

注:コレクションのどこにコードがあるのか​​分かりませんでしたので、そのブロックをどうすればよいか分かりませんでした。この例では、実行するように設定しました

+0

助けてくれてありがとうが、私はまだそれを動作させることはありません。あなたの例のコードをコピーしてください。 – thaking

関連する問題