Q:

Python callback in a class?

I found code text summarization in Github, and I will change this program became Tkinter program.I have problem when will get value in class using Button widget and show result in Text widget.How to get value of method summarize in this code use Tkinter button?I usualy only use function or procedure nothing class and method.This code already running in intrepreter.
import nltk
from nltk.tokenize import sent_tokenize
from nltk.tokenize import word_tokenize
from nltk.probability import FreqDist 
from nltk.corpus import stopwords

class NaiveSummarizer:

    def summarize(self, input, num_sentences ):

        punt_list=['.',',','!','?']
        summ_sentences = []

        sentences = sent_tokenize(input)
        lowercase_sentences =[sentence.lower() 
            for sentence in sentences]
        #print lowercase_sentences

        s=list(input)
        ts=''.join([ o for o in s if not o in  punt_list ]).split()
        lowercase_words=[word.lower() for word in ts]
        words = [word for word in lowercase_words if word not in stopwords.words()]
        word_frequencies = FreqDist(words)

        most_frequent_words = [pair[0] for pair in 
            word_frequencies.items()[:100]]

                # add sentences with the most frequent words
        for word in most_frequent_words:
            for i in range(0, len(lowercase_sentences)):
                                if len(summ_sentences) < num_sentences:
                                        if (lowercase_sentences[i] not in summ_sentences and word in lowercase_sentences[i]):
                                                summ_sentences.append(sentences[i])
                                                break

        # reorder the selected sentences
        summ_sentences.sort( lambda s1, s2: input.find(s1) - input.find(s2) )
        return " ".join(summ_sentences)

if __name__ == "__main__":

    naivesum = NaiveSummarizer()
    text='''
    To see a world in a grain of sand,
    And a heaven in a wild flower,
    Hold infinity in the palm of your hand,
    And eternity in an hour.

    A robin redbreast in a cage
    Puts all heaven in a rage.

    A dove-house fill'd with doves and pigeons
    Shudders hell thro' all its regions.
    '''
text2 = '''
         You conclude with the aphorism of Hippocrates, "Qui gravi morbo correpti dolores non sentiunt, us mens aegro­tat" (Those who do not perceive that they are wasted by seri­ous illness are sick in mind), and suggest that I am in need of medicine not only to conquer my malady, but even more, to sharpen my senses for the condition of my inner self. I would fain give you an answer such as you deserve, fain reveal myself to you entirely, but I do not know how to set about it. Hardly do I know whether I am still the same person to whom your precious letter is addressed. 
'''
print(naivesum.summarize(text2,3))
print(naivesum.summarize(text,2))

A:

You can not directly use the summarize function as a callback for your button; instead you should wrap it into another function that calls summarize and then displays the result in the Entry widget.
First, you have to add a text variable to you widget so you can read and write the text, like this:
self.outputvar = StringVar()
Entry(self, textvariable=self.outputvar)

Now you can add a callback function to your button, like in your other question, doing this:
def callback(self):
    text = "lorem ipsum" # get actual input text, maybe from another widget
    num = 3              # get proper value for whatever this is for
    result = self.summarize(text, num) # call summarize
    self.outputvar.set(result)         # show results in your widget

Alternatively, you could use a Text widget; here, inserting text is handled differently though.

