2015-09-04 136 views
1

我正在構建一個簡單的程序來比較兩個文件。我已經爲程序完成了主要代碼,現在我正在爲它實現一個GUI。如何從類中的函數打印返回值

所以我的問題出現時試圖做一個按鈕,當按下將允許用戶選擇一個文件,然後讀取該文件。這是一個功能。另一個按鈕將比較兩個文件,一個是用戶選擇的文件,另一個是另一個文件。所以這是另一個功能。所以我需要第一個函數的返回值將其輸入到另一個函數中。

from Tkinter import * 
from tkFileDialog import * 




def make_dict(data): 
    return dict((line.split(None, 1)[0], line)for line in data) 


class myButtons: 
    def __init__(self, master): 
     firstFrame = Frame(master) 
     firstFrame.pack() 

     self.openLogButton = Button(firstFrame, text="Browse", command=self.getFileInfo) 
     self.openLogButton.pack() 

     self.printButton = Button(firstFrame, text="Print", command=self.compareAction) 
     self.printButton.pack() 

     self.quitButton = Button(firstFrame, text="Quit", command=firstFrame.quit) 
     self.quitButton.pack() 

     self.scroll = Scrollbar(firstFrame) 
     self.inputText = Text(firstFrame, height=4, width=50) 

     self.scroll.pack(side=RIGHT, fill=Y) 
     self.inputText.pack(side=LEFT, fill=Y) 

     self.scroll.config(command=self.inputText.yview) 
     self.inputText.config(yscrollcommand=self.scroll.set) 

     self.logFile = self.getFileInfo() 

     thisIsTest = self.getFileInfo 

    def printMessage(self): 
     print "This works" 
     test = self.inputText.get("1.0", END) 
     print test 

    def getFileInfo(self): 
     return askopenfile(mode='rb') 

    def compareAction(self): 
     def process(infile, outfile, keywords): 
      keys = [[k[0], k[1], 0] for k in keywords] 
      endk = None 
      with open(infile, 'rb') as fdin: 
       with open(outfile, 'ab') as fdout: 
        fdout.write("<" + words + ">" + "\r\n") 
        for line in fdin: 
         if endk is not None: 
          fdout.write(line) 
          if line.find(endk) >= 0: 
           fdout.write("\r\n") 
           endk = None 
         else: 
          for k in keys: 
           index = line.find(k[0]) 
           if index >= 0: 
            fdout.write(line[index + len(k[0]):].lstrip()) 
            endk = k[1] 
            k[2] += 1 
      if endk is not None: 
       raise Exception(endk + "Not found before end of file") 
      return keys 
     start_token = self.inputText.get("1.0", END) 
     end_token = "[+][+]" 
     split_start = start_token.split(' ') 
     outputText = 'test.txt' 

     print self.logFile 

     # for words in split_start: 
     #  process(self.getFileInfo, outputText, split_start) 


root = Tk() 
b = myButtons(root) 

root.mainloop() 

對於現在我只是想測試,如果我的compareAction功能從getFileInfo功能接收到的返回值。到目前爲止,當我嘗試print self.getFileInfo我對待這個結果:<bound method myButtons.getFileInfo of <__main__.myButtons instance at 0x100863ef0>>

我相信是函數的內存地址而不是函數讀取文件時的值。

這個想法很簡單。用戶選擇要打開的文件,打開文件並在compareaction中使用返回值之後讀取並返回。

+0

看我編輯 – pydude

回答

1

我可以考慮解決這個問題的最佳方式是添加另一個函數。嘗試改變getFileInfo(個體經營)本:

def getFileInfo(self): 
    global filename 
    filename = askopenfilename() 
    return open(filename, mode="rb") 

它本質上是做同樣的事情,你以前的功能,但它使文件中全局。然後像這樣創建另一個名爲getFileName(self)的函數。

def getFileName(self): 
    return filename 

現在當你調用的過程中的作用,使用self.getFileName代替self.getFileInfo:

process(self.getFileName, outputText, split_start) 

如果你想知道爲什麼你綁定的方法的輸出,這可能是因爲你是打開文件,而不是閱讀它。本質上,當您運行print self.logFile時,它將返回一個文件對象。這是發生了什麼事,當我想你的打印我的桌面上打開的文件:

#Input 
print askopenfile(mode="rb") 

#Output 
<open file u'C:/Users/User/Desktop/stuff.txt', mode 'rb' at 0x029BA078> 

這是發生了什麼事,當我打印的文件並用閱讀():

#Input 
print askopenfile(mode="rb").read() 

#Output 
These are words in the file stuff.txt. 

本文here給出了一個關於文件以及它們如何工作的好主意。還請記住在完成閱讀後關閉文件以防止其他問題。

0

您只需要將print self.getFileInfo更改爲print self.getFileInfo()。請記住,使用括號是你如何調用函數。也可以在代碼中任何其他位置執行此操作。請注意,這將再次調用該函數。您可以改爲使用print self.logFile查看結果。

+0

我累這個問題是這個會重新執行一次函數。所以如果我點擊compareAction按鈕,它會提示我選擇一個文件。我不想要那個按鈕來做到這一點,我只希望它「在其函數內部執行任何操作」。使用'print self.getFileInfo()'在這裏不會爲我工作。 – JeanP

+0

當我使用'self.logFile'的時候,我總是收到函數'>'的地址,也許我錯誤地從'getFileInfo'函數返回了一個值? – JeanP

+0

您是否將self.logfile = getFileInfo更改爲self.logfile = getFileInfo()? – pydude