glade+pygtk学习 (2)

按照计划,今天参照这篇文章 Building an Application with PyGTK and Glade 学习。第一次使用menu和toolbar,进一步学习了tree/list view的使用,昨天还不太会用的dialog也学会了。

下载源代码: pygtk-learning-pywine.tgz

昨天遗留的几个问题也解决了:
1. 首先完成了那个dialog。虽然没什么功能,就算是复习一下dialog的实现方法吧。
2. 把ServerInfo程序的treeview内容改成显示http的header了。

  1. #get http headers, then insert into tree model
  2. data = page.read()
  3. headers = page.info()
  4. for name in headers.keys():
  5.     insert_row(self.treemodel, None, name, headers[name])

3. 把页面内容转换成unicode字符。用到了re模块去取页面meta标记里的字符集设置。

  1. import re, string
  2.        
  3. r = re.compile(r""".*content=['|"]?text/html;\s?charset=([a-zA-Z0-9\-]+)['|"]?.*""", re.S | re.I | re.M)
  4. m = r.match(data)
  5. if m:
  6.     charset = string.lower(m.group(1))
  7.     if charset != 'utf-8':
  8.         data = data.decode(charset, 'ignore').encode('utf-8', 'ignore')
  9.     else:
  10.         data = data.encode('utf-8', 'ignore')
  11. self.logwindow.set_text(data)

4. 用TexBuffer的set_text方法替代了insert_at_cursor。

新ServerInfo程序的源代码: pygtk-learning-serverinfo.tgz

Leave a Reply

Your email address will not be published. Required fields are marked *

*

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.
For more help see http://daringfireball.net/projects/markdown/syntax

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>