Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:44:05+00:00 2026-05-23T16:44:05+00:00

I’m new to programing, this is my first python-gtk applet and I’m trying to

  • 0

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].split(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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:44:06+00:00Added an answer on May 23, 2026 at 4:44 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.