开发者

Python - GTK - applet similar to gnome dictionary error

I'm new to programing, this is my first python-gtk applet and I'm trying to make an applet similar to gnome-dictionary that retrieves the word meaning from the site http://www.priberam.pt/dlpo/. I'm doing it little-by-little but now I'm stuck, can someone help me to see what am I doing wrong?

I get this error:

"TypeError: unbound method enter_callback() must be called with x instance as first argument (got Entry instance instead)"

The code is as follows:

from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
import urllib2
import re

import HTMLParser
import sys
import gtk
import pango
import string



class x:
    def enter_callback(self, widget, entry):
    entry_text = entry.get_text()
    wordTodefine = entry_text
    url = "http://www.priberam.pt/dlpo/dlpo.aspx?pal="

    url = '{0}{1}'.format(url, wordTodefine)


    g = urllib2.urlopen(url)


    s = g.read()



    def extract(text, sub1, sub2):
        """extract a substring between two substrings sub1 and sub2 in text"""
        return text.split(sub1)[-1].开发者_StackOverflowsplit(sub2)[0]




    str4 = extract(s, ' <?xml version="1.0" encoding="utf-16"?><div><table style="background-color:#eee; width:100%;" cellpadding="4" cellspacing="0" border="0" bordercolor="#cccccc"><tr><td><div>', '<div id="ctl00_ContentPlaceHolder1_pnl_relacionadas">')

    str5 = '{0}{1}{2}'.format('<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><div><table style="background-color:#eee; width:100%;" cellpadding="4" cellspacing="0" border="0" bordercolor="#cccccc"><tr><td><div>', str4, '</html>')

    return str5



class HTMLBuffer(HTMLParser.HTMLParser):
    ignoreTags = ('title', 'table')
    noTagTags = ('html', 'head')
    newlineTags = ('p', 'h1', 'h2', 'li', 'div')
    whiteSpaceNuker = re.compile(r"""\s+""", re.MULTILINE) 
    def __init__(self):
    self.buffer = gtk.TextBuffer()
    self.ignoreData = 0
    self.inList = 0
    self.currentTag = ''
    self.startOfP = 0
    HTMLParser.HTMLParser.__init__(self)
    if gtk.gdk.screen_width() >= 800:
        baseSize = 13
    else:
        baseSize = 10

    baseFont = 'Times'

    tag = self.buffer.create_tag('body')
    tag.set_property('font', '%s %d' % (baseFont, baseSize))

    tag = self.buffer.create_tag('p')
    tag.set_property('pixels-above-lines', 5)
    tag.set_property('pixels-below-lines', 5)

    tag = self.buffer.create_tag('tt')
    tag.set_property('font', 'Times %d' % (baseSize,))

    tag = self.buffer.create_tag('a')
    tag.set_property('font', '%s %d' % (baseFont, baseSize))

    tag = self.buffer.create_tag('h1')
    tag.set_property('font', '%s %d' % (baseFont, baseSize + 10))
    tag.set_property('weight', pango.WEIGHT_BOLD)        

    tag = self.buffer.create_tag('h2')
    tag.set_property('font', '%s %d' % (baseFont, baseSize + 4))
    tag.set_property('weight', pango.WEIGHT_BOLD)

    tag = self.buffer.create_tag('b')
    tag.set_property('weight', pango.WEIGHT_BOLD)

    tag = self.buffer.create_tag('i')
    tag.set_property('style', pango.STYLE_ITALIC)

    tag = self.buffer.create_tag('em')
    tag.set_property('style', pango.STYLE_ITALIC)

    tag = self.buffer.create_tag('ul')
    tag.set_property('left-margin', 20)
    # reset spacing in paragraphs incase this list is inside <p>
    tag.set_property('pixels-above-lines', 0)
    tag.set_property('pixels-below-lines', 0)

    tag = self.buffer.create_tag('li')
    tag.set_property('indent', -9)

    self.iter = self.buffer.get_iter_at_offset(0)
    self.offsets = {}

    def get_buffer(self):
    return self.buffer

    def pushTag(self, tag, offset):
    if self.offsets.has_key(tag):
        self.offsets[tag].append(offset)
    else:
        self.offsets[tag] = [offset]

    def popTag(self, tag):
    if not self.offsets.has_key(tag):
        raise RuntimeError, "impossible"
    return self.offsets[tag].pop()

    # structure markup
    def handle_starttag(self, tag, attrs):
    if tag in self.ignoreTags:
        self.ignoreData += 1
        return
    self.currentTag = tag
    if tag in self.noTagTags:
        return
    self.pushTag(tag, self.iter.get_offset())
    if tag == 'li':
        self.inList += 1
        self.buffer.insert(self.iter, u'\u2022 ')
    elif tag == 'p':
        self.startOfP = 1

    def handle_endtag(self, tag):
    if tag in self.ignoreTags:
        self.ignoreData -= 1
        return
    if tag == 'li':
        self.inList -= 1
    if tag in self.noTagTags:
        return
    offset = self.popTag(tag)
    current = self.iter.get_offset()
    if tag in self.newlineTags and offset != current:
        if tag == 'p' and self.inList:
            offset -= 2
        # put a newline at the beginning
        start = self.buffer.get_iter_at_offset(offset)
        self.buffer.insert(start, '\n')
        offset += 1
        current += 1
        self.iter = self.buffer.get_iter_at_offset(current)
    start = self.buffer.get_iter_at_offset(offset)
    self.buffer.apply_tag_by_name(tag, start, self.iter)

    # all other markup
    def handle_data(self, data):
    if self.ignoreData == 0:
        data = data.replace('\n', ' ')
        data = self.whiteSpaceNuker.sub(' ', data)
        if self.startOfP:
            if data.startswith(' '):
                data = data[1:]
            self.startOfP = 0
        #print '|%s|' % (data,)
        self.buffer.insert(self.iter, data)

if __name__ == '__main__':
    def quit(*args):
    gtk.main_quit()


    buffer = HTMLBuffer()
    buffer.feed(x)
    buffer.close()
#if __name__ == '__main__':
#def __init__():
    window = gtk.Window()
    vbox = gtk.VBox(False, 0)
    view = gtk.TextView()

    view.set_property("editable", False)
    view.set_property("cursor_visible", False)

    entry = gtk.Entry()

    entry.connect("activate", x.enter_callback, entry, view)
    vbox.pack_start(entry, False, False, 0)
    vbox.pack_end(view, False, False, 0)

    window.connect("destroy", lambda w: gtk.main_quit())

    window.add(vbox)
    window.show_all()


x()
gtk.main()

I used an HtmlParser made by Matt Wilson and tried to integrate it in my file...

Thanks in advance, and sorry for the mess that this code is.


Why is the function enter_callback a method of the class x? It doesn't seem like there is any good structural reason for it to be in x in the first place. Take it out of x and the error message will go away (the error message is complaining that self isn't being passed to enter_callback). Well, at least this one will go away, probably replaced by another one :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜