Python - GTK - 小程序类似于 gnome 字典错误
我是编程新手,这是我的第一个 python-gtk 小程序,我正在尝试制作一个类似于 gnome-dictionary 的小程序,它从网站 http://www.priberam.pt/dlpo/。我正在一点一点地做,但现在我陷入困境,有人可以帮助我看看我做错了什么吗?
我收到此错误:
“TypeError:必须使用 x 实例作为第一个参数调用未绑定方法 Enter_callback()(改为获取 Entry 实例)”
代码如下:
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()
我使用了 Matt Wilson 制作的 HtmlParser 并尝试将其集成到我的文件...
提前致谢,并对这段代码造成的混乱表示歉意。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么函数
enter_callback
是类x
的方法?首先似乎没有任何好的结构原因让它出现在x
中。将其从x
中取出,错误消息就会消失(错误消息抱怨self
未传递给enter_callback
)。好吧,至少这个会消失,可能会被另一个取代:)Why is the function
enter_callback
a method of the classx
? It doesn't seem like there is any good structural reason for it to be inx
in the first place. Take it out ofx
and the error message will go away (the error message is complaining thatself
isn't being passed toenter_callback
). Well, at least this one will go away, probably replaced by another one :)