我正在構建一個簡單的程序來比較兩個文件。我已經爲程序完成了主要代碼,現在我正在爲它實現一個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中使用返回值之後讀取並返回。
看我編輯 – pydude