Python和科学计算认证群组  - 讨论区

标题:A ReStructuredText Editor

2012年02月14日 星期二 16:39

https://svn.enthought.com/svn/enthought/Rested/trunk/

 

一个ReStructuredText 的编辑器

 

调试了很久,只能显示如此程度

 

中间的代码框和右侧的HTML显示框显示有问题

Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_notifiers.py", line 488, in rebind_call_1
    self.dispatch( getattr( self.object(), self.name ), new )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_notifiers.py", line 598, in dispatch
    handler( *args )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/list_editor.py", line 560, in update_editor_item
    ui, view_object, monitoring  = self._create_page(object)
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/list_editor.py", line 654, in _create_page
    kind   = factory.ui_kind ).set(
  File "/usr/lib/python2.6/dist-packages/enthought/traits/has_traits.py", line 2241, in edit_traits
    handler, id, scrollable, args )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/view.py", line 429, in ui
    ui.ui( parent, kind )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/ui.py", line 158, in ui
    self.rebuild( self, parent )
  File "/usr/local/lib/python2.6/dist-packages/TraitsBackendQt-3.6.1.dev_r26439-py2.6.egg/enthought/traits/ui/qt4/toolkit.py", line 111, in ui_panel
    ui_panel.ui_panel( ui, parent )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/ui_panel.py", line 70, in ui_panel
    _ui_panel_for(ui, parent, False)
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/ui_panel.py", line 91, in _ui_panel_for
    ui.prepare_ui()
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/ui.py", line 309, in prepare_ui
    if handler.init( info ) == False:
  File "/home/rosickey/Rsted/enthought/rst/rest_editor_view.py", line 110, in init
    self.code_widget.code.keyPressEvent_action = self.keyPressEvent_action
AttributeError: 'NoneType' object has no attribute 'code'
[ERROR] trait_notifiers.py:244 Exception occurred in traits notification handler for object: <rest_editor_view.ReSTHTMLEditorView object at 0xb77e198c>, trait: open_views_items, old value: <undefined>, new value: <enthought.traits.trait_handlers.TraitListEvent object at 0x9e0dfec>
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_notifiers.py", line 488, in rebind_call_1
    self.dispatch( getattr( self.object(), self.name ), new )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_notifiers.py", line 598, in dispatch
    handler( *args )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/list_editor.py", line 560, in update_editor_item
    ui, view_object, monitoring  = self._create_page(object)
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/list_editor.py", line 654, in _create_page
    kind   = factory.ui_kind ).set(
  File "/usr/lib/python2.6/dist-packages/enthought/traits/has_traits.py", line 2241, in edit_traits
    handler, id, scrollable, args )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/view.py", line 429, in ui
    ui.ui( parent, kind )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/ui.py", line 158, in ui
    self.rebuild( self, parent )
  File "/usr/local/lib/python2.6/dist-packages/TraitsBackendQt-3.6.1.dev_r26439-py2.6.egg/enthought/traits/ui/qt4/toolkit.py", line 111, in ui_panel
    ui_panel.ui_panel( ui, parent )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/ui_panel.py", line 70, in ui_panel
    _ui_panel_for(ui, parent, False)
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/ui_panel.py", line 91, in _ui_panel_for
    ui.prepare_ui()
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/ui.py", line 309, in prepare_ui
    if handler.init( info ) == False:
  File "/home/rosickey/Rsted/enthought/rst/rest_editor_view.py", line 110, in init
    self.code_widget.code.keyPressEvent_action = self.keyPressEvent_action
AttributeError: 'NoneType' object has no attribute 'code'

 

注释掉 rest_editor_view.py 中的

self.code_widget.code.keyPressEvent_action = self.keyPressEvent_action

可以显示如图的界面

 

 

 

2012年02月14日 星期二 18:38

try:

    self.code_widget.code.keyPressEvent_action = self.keyPressEvent_action

except:

    ....

 

or

 

if self.code_widget:

    self.code_widget.code.keyPressEvent_action = self.keyPressEvent_action

2012年02月14日 星期二 19:21

root@rosickey:/home/rosickey/Rsted/enthought/rst# python app.py
The rest editor only supports qt4 as toolkit. Toolkit changed to qt4.
/usr/lib/python2.6/dist-packages/enthought/etsconfig/etsconfig.py:343: UserWarning: Environment variable "HOME" not set, setting home directory to /tmp
  (environment_variable, parent_directory))
code widget None
Exception occurred in traits notification handler.
Please check the log file for details.
Exception occurred in traits notification handler for object: <rest_editor_view.ReSTHTMLPairView object at 0xa03483c>, trait: _change_font_action, old value: <undefined>, new value: True
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_notifiers.py", line 481, in rebind_call_0
    self.dispatch( getattr( self.object(), self.name ) )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_notifiers.py", line 598, in dispatch
    handler( *args )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/ui.py", line 926, in dispatch
    self.method( self.info )
  File "/home/rosickey/Rsted/enthought/rst/rest_editor_view.py", line 173, in object__change_font_action_changed
    self.code_widget.code.set_font(font)
AttributeError: 'NoneType' object has no attribute 'code'
[ERROR] trait_notifiers.py:244 Exception occurred in traits notification handler for object: <rest_editor_view.ReSTHTMLPairView object at 0xa03483c>, trait: _change_font_action, old value: <undefined>, new value: True
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_notifiers.py", line 481, in rebind_call_0
    self.dispatch( getattr( self.object(), self.name ) )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_notifiers.py", line 598, in dispatch
    handler( *args )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/ui.py", line 926, in dispatch
    self.method( self.info )
  File "/home/rosickey/Rsted/enthought/rst/rest_editor_view.py", line 173, in object__change_font_action_changed
    self.code_widget.code.set_font(font)
AttributeError: 'NoneType' object has no attribute 'code'
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/TraitsBackendQt-3.6.1.dev_r26439-py2.6.egg/enthought/traits/ui/qt4/toolkit.py", line 75, in event
    self._handler(*self._args)
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/ui.py", line 926, in dispatch
    self.method( self.info )
  File "/home/rosickey/Rsted/enthought/rst/rest_editor_view.py", line 186, in object__set_html_pos_action_changed
    html_frame = self.html_control.page().mainFrame()
AttributeError: 'NoneType' object has no attribute 'page'

中间的代码框还是图上显示的那样,只有一行,不正常

2012年02月14日 星期二 19:25

你可以根据错误信息提示,在出错的地方加上判断。

2012年02月14日 星期二 19:27

都像第一个一样加了,不报错,但还是无法编辑只有一行

 

root@rosickey:/home/rosickey/Rsted/enthought/rst# python app.py
The rest editor only supports qt4 as toolkit. Toolkit changed to qt4.
/usr/lib/python2.6/dist-packages/enthought/etsconfig/etsconfig.py:343: UserWarning: Environment variable "HOME" not set, setting home directory to /tmp
  (environment_variable, parent_directory))
code widget None

2012年02月14日 星期二 20:05

提示这个错误的话,应该是需要指定qt4为后台界面库吧。在app.py中添加:

import os
os.envion['ETS_TOOLKIT'] = 'qt4'

2012年02月14日 星期二 20:26

恩,已经添加了。

 

显示还是一行,无法编辑

root@rosickey:/home/rosickey/Rsted/enthought/rst# python app.py
/usr/lib/python2.6/dist-packages/enthought/etsconfig/etsconfig.py:370: UserWarning: Environment variable "HOME" not set, setting home directory to /tmp
  (environment_variable, parent_directory))
code widget Non

2012年02月14日 星期二 20:37

那个编辑器应该是用CodeEditor生成的,你可以先单独写一个测试程序看看它能否正常工作。

2012年02月14日 星期二 20:43

修改了rest_editor_view.py的458行

加了个height值,源码框是变大了,但是能编辑的地方还是一行...

        return View(Group(Group(Item('object.model.rest',
                                     style='custom',
                                     height = 600,

2012年02月14日 星期二 20:50

我想因为这个rest实际使用的编辑器应该是一个单行的文本框。所以你可以试试其他的editor。例如把editor=rest_editor删除试试看,或者单独写一个程序测试那个CodeEditor。或者看看traits的例子中的CodeEditor能否正常工作。或者从例子中找出一个能够进行多行编辑的编辑器。

2012年02月14日 星期二 20:55

谢谢了,果然这样。

删掉editor=rest_editor就可以了

        return View(Group(Group(Item('object.model.rest',
                                     style='custom',
                                     ),

...

为什么删除了就是多行了

2012年02月14日 星期二 21:24

不指定editor,就用Str对应的缺省的custom编辑器,这个编辑器就是多行的。这也就是说CodeEditor在Qt4下无法正常工作。也许要升级一下TraitsUI

2012年02月14日 星期二 21:36

恩,这样就少了一些行号,颜色之类的

2012年02月15日 星期三 11:54

  中文路径有有乱码

File "/home/rosickey/Rsted/enthought/rst/file_tree.py", line 51, in _get_children
    names = [str(f) for f in os.listdir(self.path)]
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

改成

        for f in os.listdir(self.path):
            if isinstance(f,unicode):
                names.append(str(f.encode("gb2312")))
            else:
                names.append(str(f.decode('utf-8').encode('gb2312')))
 会出现下边的错

 

  File "/usr/lib/python2.6/dist-packages/enthought/traits/has_traits.py", line 1279, in decorator
    self.__dict__[ name ] = result = function( self )
  File "/home/rosickey/Rsted/enthought/rst/file_tree.py", line 61, in _get_children
    path = os.path.join(self.path, fn)
  File "/usr/lib/python2.6/posixpath.py", line 70, in join
    path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbd in position 1: ordinal not in range(128)

这个地方字符编码怎么改呢

2012年02月15日 星期三 13:37

编码问题改起来比较费劲,首先应该都用unicode,因此要看看这里的self.path是不是unicode,如果不是,就需要把它改为unicode:

names = [str(f) for f in os.listdir(self.path)]

这样os.listdir()所得到的也是unicode,unicode应该能在界面中正常显示。

程序中凡是不用unicode的地方都需要改过来。

2012年02月15日 星期三 13:50

哲思,刚才修改的没显示。

self.path也是str

selfpathtthththth /home/rosickey/下载 <type 'str'>
selfpathtthththth /home/rosickey/EnthoughtBase <type 'str'>
selfpathtthththth /home/rosickey/mouse <type 'str'>
selfpathtthththth /home/rosickey/视频 <type 'str'>

显示了每个f的type何内容

        for f in os.listdir(self.path):
            print  'typessssssssssssssss',type(f)
            print f

结果type都已经是str了。下边有一句

names.sort(key=str.lower)排序

有中文就会报错type不对,但是之前的f类型已经是str了的

print 输出中文也正常。

~~~~~~~~~~~~~~

试着直接names = [(f) for f in os.listdir(self.path)]这样了,

注释掉了排序 names.sort(key=str.lower)

刚打开界面还是中文路径乱码,手动选择路径之后就正常了,不知到怎么个意思

点击乱码中文的节点,显示

rosickey@rosickey:~/Rsted/enthought/rst$ sudo python app.py
/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/tree_editor.py:1037: UnicodeWarning: Unicode unequal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  if new_label != old_label:
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/qt4/tree_editor.py", line 1039, in _on_nid_changed
    node.set_label(object, new_label)
  File "/usr/lib/python2.6/dist-packages/enthought/traits/ui/tree_node.py", line 298, in set_label
    xsetattr( object, label_name, label )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_base.py", line 509, in xsetattr
    setattr( object, names[-1], value )
  File "/usr/lib/python2.6/dist-packages/enthought/traits/trait_handlers.py", line 99, in _read_only
    name, class_of( object ) )
enthought.traits.trait_errors.TraitError: The 'name' trait of a DirectoryNode instance is 'read only'.

2012年09月04日 星期二 18:19

中文目录果然乱码。我的编辑功能功能倒是很正常。(ubuntu10.10 & python2.6)

楼主现在还在用这个编辑器吗,可以交流一下?

2012年09月05日 星期三 09:19

在用,(ubuntu10.10 & python2.6) me too。。。

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号