【问题标题】:Python callback in a class? [closed]类中的Python回调? [关闭]
【发布时间】:2014-04-24 07:48:37
【问题描述】:

我在 Github 中找到代码文本摘要,我将把这个程序变成 Tkinter 程序。我有问题何时使用 Button 小部件在类中获取值并在 Text 小部件中显示结果。如何在此代码中获取方法汇总的值使用 Tkinter 按钮?我通常只使用函数或过程,没有类和方法。这段代码已经在解释器中运行。

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))

【问题讨论】:

  • 你为什么不从你的other question 中添加代码,什么不工作呢?不要指望我们为您编写整个 GUI。
  • 我不是这个意思。我只要求提供相同问题的示例代码。我可以从代码中学习应用到我的案例中使用类,并使用 tkinter 或网站地址获取类的值以供我学习,非常感谢
  • 但在您的其他问题中,您已经向按钮添加了回调。为什么不能用你的callback 函数调用summarize 函数并将结果放入文本字​​段?
  • 我已经实现了我之前通过总结代码文本询问的代码。并出现以下错误: Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):我的代码发生了什么?请和我解释一下,谢谢 File "C:\Python27\lib\lib-tk\Tkinter.py",第 1410 行, 在 call 中返回 self.func(*args) TypeError: summarise() 正好需要 3 个参数(1 个给定)
  • 我是python新手,请多多解释,谢谢

标签: python class tkinter


【解决方案1】:

您不能直接使用summarize 函数作为按钮的回调;相反,您应该将其包装到另一个调用 summarize 的函数中,然后在 Entry 小部件中显示结果。

首先,您必须向您的小部件添加一个text variable,以便您可以读取和写入文本,如下所示:

self.outputvar = StringVar()
Entry(self, textvariable=self.outputvar)

现在您可以将callback 函数添加到您的按钮,就像在your other question 中一样,这样做:

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

或者,您可以使用Text 小部件;在这里,inserting text 的处理方式有所不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    相关资源
    最近更新 更多