2010-12-12 79 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() 

,用于快速创建简单的GUI程序,我强烈建议EasyGUI,一个相当强大且简单Tk以诚为本Python模块做这样的事情。

+0

非常感谢;)寻求帮助 – thaking 2010-12-12 23:34:39

+0

3分钟后打出来...... +1好的答案,也比我的复杂得多。 – John 2010-12-12 23:36:37

0

在你的openFile()函数中,就在你向用户提出一个文件名后,把你的代码!

+0

那么这是行不通的,我试过了,但是当我运行它时,首先打开窗口对话框,打开text.file。我想要按钮,然后点击它并打开.txt文件,然后在“代码”中执行此操作。 – thaking 2010-12-12 22:44:11

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.

编辑:的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 2010-12-12 23:16:47