我正在用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