我正在用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()
現在我不知道如何組裝我的代碼,所以它按下按鈕時運行。
非常感謝;)尋求幫助 – thaking 2010-12-12 23:34:39
3分鐘後打出來...... +1好的答案,也比我的複雜得多。 – John 2010-12-12 23:36:37