2010-12-12 90 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