configure_traits 在 Spyder/Anaconda 中冻结控制台
configure_traits freezes console in Spyder/Anaconda
尝试 traitsui 文档中的第一个示例:
from traits.api import HasTraits, Str, Int
from traitsui.api import View, Item
import traitsui
class SimpleEmployee(HasTraits):
first_name = Str
last_name = Str
department = Str
employee_number = Str
salary = Int
view1 = View(Item(name = 'first_name'),
Item(name = 'last_name'),
Item(name = 'department'))
sam = SimpleEmployee()
sam.configure_traits(view=view1)
使 Spyder IPython 控制台挂起,即使 UI window 已关闭。
MacOSX 10.14.6、Spyder 4.0.0、Python3.7.0、IPython7.10.2、traitsui 6.1.3
可能需要配置一些关于 UI 事件循环的东西,但是是什么以及如何配置?
Canopy IPython 控制台和 Spyder IPython 控制台的区别在于
app = QtGui.QApplication.instance()
print(app)
returns
<PyQt5.QtWidgets.QApplication at 0xXXXXXXXX>
两者都是,但是
is_event_loop_running_qt4(app)
returns True
在 Canopy IPython 终端和 False
在 Spyder IPython 终端。
有必要在 traits.qt4.view_application 中修补 view_application()
,方法是注释掉开始新 ViewApplication
实例的行,具体取决于此测试。可以 运行 以下代码代替更改原始文件:
# -*- coding: utf-8 -*-
"""Avoid Spyder console to hang when using configure_traits()
"""
from IPython import get_ipython
from pyface.qt import QtCore, QtGui, qt_api
from pyface.util.guisupport import is_event_loop_running_qt4
from traitsui.api import toolkit
from traitsui.qt4.view_application import ViewApplication
KEEP_ALIVE_UIS = set()
def on_ui_destroyed(object, name, old, destroyed):
""" Remove the UI object from KEEP_ALIVE_UIS.
"""
assert name == 'destroyed'
if destroyed:
assert object in KEEP_ALIVE_UIS
KEEP_ALIVE_UIS.remove(object)
object.on_trait_change(on_ui_destroyed, 'destroyed', remove=True)
def _view_application(context, view, kind, handler, id, scrollable, args):
""" Creates a stand-alone PyQt application to display a specified traits UI
View.
Parameters
----------
context : object or dictionary
A single object or a dictionary of string/object pairs, whose trait
attributes are to be edited. If not specified, the current object is
used.
view : view object
A View object that defines a user interface for editing trait attribute
values.
kind : string
The type of user interface window to create. See the
**traitsui.view.kind_trait** trait for values and
their meanings. If *kind* is unspecified or None, the **kind**
attribute of the View object is used.
handler : Handler object
A handler object used for event handling in the dialog box. If
None, the default handler for Traits UI is used.
scrollable : Boolean
Indicates whether the dialog box should be scrollable. When set to
True, scroll bars appear on the dialog box if it is not large enough
to display all of the items in the view at one time.
"""
if (kind == 'panel') or ((kind is None) and (view.kind == 'panel')):
kind = 'modal'
# !!!!!!! The following 4 lines commented out !!!!!!!!!!!
# app = QtGui.QApplication.instance()
# if app is None or not is_event_loop_running_qt4(app):
# return ViewApplication(context, view, kind, handler, id,
# scrollable, args).ui.result
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ui = view.ui(context,
kind=kind,
handler=handler,
id=id,
scrollable=scrollable,
args=args)
# If the UI has not been closed yet, we need to keep a reference to
# it until it does close.
if not ui.destroyed:
KEEP_ALIVE_UIS.add(ui)
ui.on_trait_change(on_ui_destroyed, 'destroyed')
return ui.result
def view_application(self, context, view, kind=None, handler=None,
id='', scrollable=None, args=None):
""" Creates a PyQt modal dialog user interface that
runs as a complete application, using information from the
specified View object.
Parameters
----------
context : object or dictionary
A single object or a dictionary of string/object pairs, whose trait
attributes are to be edited. If not specified, the current object is
used.
view : view or string
A View object that defines a user interface for editing trait
attribute values.
kind : string
The type of user interface window to create. See the
**traitsui.view.kind_trait** trait for values and
their meanings. If *kind* is unspecified or None, the **kind**
attribute of the View object is used.
handler : Handler object
A handler object used for event handling in the dialog box. If
None, the default handler for Traits UI is used.
id : string
A unique ID for persisting preferences about this user interface,
such as size and position. If not specified, no user preferences
are saved.
scrollable : Boolean
Indicates whether the dialog box should be scrollable. When set to
True, scroll bars appear on the dialog box if it is not large enough
to display all of the items in the view at one time.
"""
return _view_application(context, view, kind, handler,
id, scrollable, args)
from traitsui.qt4.toolkit import GUIToolkit
GUIToolkit.view_application = view_application
因此,该行为在 Spyder 和 Canopy IPython 控制台中都是正常的。请注意 configure_traits(kind='modal')
也可以。
尝试 traitsui 文档中的第一个示例:
from traits.api import HasTraits, Str, Int
from traitsui.api import View, Item
import traitsui
class SimpleEmployee(HasTraits):
first_name = Str
last_name = Str
department = Str
employee_number = Str
salary = Int
view1 = View(Item(name = 'first_name'),
Item(name = 'last_name'),
Item(name = 'department'))
sam = SimpleEmployee()
sam.configure_traits(view=view1)
使 Spyder IPython 控制台挂起,即使 UI window 已关闭。
MacOSX 10.14.6、Spyder 4.0.0、Python3.7.0、IPython7.10.2、traitsui 6.1.3
可能需要配置一些关于 UI 事件循环的东西,但是是什么以及如何配置?
Canopy IPython 控制台和 Spyder IPython 控制台的区别在于
app = QtGui.QApplication.instance()
print(app)
returns
<PyQt5.QtWidgets.QApplication at 0xXXXXXXXX>
两者都是,但是
is_event_loop_running_qt4(app)
returns True
在 Canopy IPython 终端和 False
在 Spyder IPython 终端。
有必要在 traits.qt4.view_application 中修补 view_application()
,方法是注释掉开始新 ViewApplication
实例的行,具体取决于此测试。可以 运行 以下代码代替更改原始文件:
# -*- coding: utf-8 -*-
"""Avoid Spyder console to hang when using configure_traits()
"""
from IPython import get_ipython
from pyface.qt import QtCore, QtGui, qt_api
from pyface.util.guisupport import is_event_loop_running_qt4
from traitsui.api import toolkit
from traitsui.qt4.view_application import ViewApplication
KEEP_ALIVE_UIS = set()
def on_ui_destroyed(object, name, old, destroyed):
""" Remove the UI object from KEEP_ALIVE_UIS.
"""
assert name == 'destroyed'
if destroyed:
assert object in KEEP_ALIVE_UIS
KEEP_ALIVE_UIS.remove(object)
object.on_trait_change(on_ui_destroyed, 'destroyed', remove=True)
def _view_application(context, view, kind, handler, id, scrollable, args):
""" Creates a stand-alone PyQt application to display a specified traits UI
View.
Parameters
----------
context : object or dictionary
A single object or a dictionary of string/object pairs, whose trait
attributes are to be edited. If not specified, the current object is
used.
view : view object
A View object that defines a user interface for editing trait attribute
values.
kind : string
The type of user interface window to create. See the
**traitsui.view.kind_trait** trait for values and
their meanings. If *kind* is unspecified or None, the **kind**
attribute of the View object is used.
handler : Handler object
A handler object used for event handling in the dialog box. If
None, the default handler for Traits UI is used.
scrollable : Boolean
Indicates whether the dialog box should be scrollable. When set to
True, scroll bars appear on the dialog box if it is not large enough
to display all of the items in the view at one time.
"""
if (kind == 'panel') or ((kind is None) and (view.kind == 'panel')):
kind = 'modal'
# !!!!!!! The following 4 lines commented out !!!!!!!!!!!
# app = QtGui.QApplication.instance()
# if app is None or not is_event_loop_running_qt4(app):
# return ViewApplication(context, view, kind, handler, id,
# scrollable, args).ui.result
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ui = view.ui(context,
kind=kind,
handler=handler,
id=id,
scrollable=scrollable,
args=args)
# If the UI has not been closed yet, we need to keep a reference to
# it until it does close.
if not ui.destroyed:
KEEP_ALIVE_UIS.add(ui)
ui.on_trait_change(on_ui_destroyed, 'destroyed')
return ui.result
def view_application(self, context, view, kind=None, handler=None,
id='', scrollable=None, args=None):
""" Creates a PyQt modal dialog user interface that
runs as a complete application, using information from the
specified View object.
Parameters
----------
context : object or dictionary
A single object or a dictionary of string/object pairs, whose trait
attributes are to be edited. If not specified, the current object is
used.
view : view or string
A View object that defines a user interface for editing trait
attribute values.
kind : string
The type of user interface window to create. See the
**traitsui.view.kind_trait** trait for values and
their meanings. If *kind* is unspecified or None, the **kind**
attribute of the View object is used.
handler : Handler object
A handler object used for event handling in the dialog box. If
None, the default handler for Traits UI is used.
id : string
A unique ID for persisting preferences about this user interface,
such as size and position. If not specified, no user preferences
are saved.
scrollable : Boolean
Indicates whether the dialog box should be scrollable. When set to
True, scroll bars appear on the dialog box if it is not large enough
to display all of the items in the view at one time.
"""
return _view_application(context, view, kind, handler,
id, scrollable, args)
from traitsui.qt4.toolkit import GUIToolkit
GUIToolkit.view_application = view_application
因此,该行为在 Spyder 和 Canopy IPython 控制台中都是正常的。请注意 configure_traits(kind='modal')
也可以。