2011年08月01日 星期一 10:22
凑了一段代码,想要实现弹出一个界面,但是界面之间怎么没有联系呢
弹出的界面事件理应和主界面时一样的
要怎么改才行呢
-*- coding: utf-8 -*-
from enthought.traits.api import HasTraits, List, Button, File, Float, Instance
from enthought.traits.ui.api import Group, Handler, View, Item, CheckListEditor,VGroup
from enthought.chaco.api import Plot, ArrayPlotData, VPlotContainer
from enthought.traits.ui.menu import Action, ActionGroup, Menu, MenuBar
from enthought.enable.component_editor import ComponentEditor
import numpy as np
class Shape(HasTraits):
pass
class aa(HasTraits):
shape = Instance(Shape)
plot_show = Instance(VPlotContainer)
plot = Instance(VPlotContainer)
a = Button()
b = List( editor = CheckListEditor( values = [ '1', '2', '3', '4' ], cols = 4 ) )
option_menu = Menu(
ActionGroup(
Action(id="open", name=u"demo", action="open_file"),
),
ActionGroup(
Action(id="exit_app", name=u"退出", action="exit_app"),
),
name = u"设置"
)
view =View(
VGroup(
Item('plot_show',editor=ComponentEditor(), show_label=False,width = 450,height = 550),
Item( 'b', style = 'simple', label = u'demo' ),
Item('a')
),
menubar = MenuBar(option_menu),
resizable = False,width = 600,title = u'demo'
)
def __init__(self):
x = np.linspace(-14, 14, 100)
y = np.sin(x) * x**3 / 1000
plotdata = ArrayPlotData(x=x, y=y)
self.p1 = Plot(plotdata, padding=30, )
self.p1.title = "Xx"
self.p1.plot(("x", "y"), type="line", color="green", bgcolor = "black",line_width=2.0,name = 'G:Xx')
self.p2 = Plot(plotdata, padding=30)
self.p2.title = "Xz"
self.p2.plot(("x", "y"), type="line", color="green", bgcolor = "black",line_width=2.0,name = 'G:Xz')
self.c1 = VPlotContainer(self.p2,self.p1,)
self.plot = self.c1
#self.plot_show = self.plot
def open_file(self):
self.shape = Options()
self.plot_show = self.plot
self.liangcheng = ['2']
self.shape.configure_traits()
def _a_fired(self):
print '2'
xx = np.random.random((5,))
yy = np.random.random((5,))
plotdata = ArrayPlotData(x=xx, y=yy)
self.p1 = Plot(plotdata, padding=40)
self.p2 = Plot(plotdata, padding=40)
self.p1.title = "X"
self.p2.title = "Y"
self.p1.plot(("x", "y"), type="line", color="red",line_width=1.0)
self.p2.plot(("x", "y"), type="line", color="red",line_width=1.0)
c1 = VPlotContainer(self.p2,self.p1)
self.plot = c1
self.smooth_check = False
self.plot_show = self.plot
class Options(Shape,aa):
view = View(
Group(
Item( 'b', style = 'simple', label = u'demo' ),
Item('a',label = u'demo',show_label=False),
label = u'demo', show_border = True,
),
)
def _a_fired(self):
print '1'
aa()._a_fired()
if __name__ == "__main__":
a = aa()
a.configure_traits()
2011年08月01日 星期一 12:21
Options不应该从aa继承,aa()._a_fired()这样是不行的,这样你是创建了一个新的aa实例,它和a = aa()创建的那个是不同的。有两种方法,一种是在Option类中添加一个保存aa实例的属性,然后在self.shape.configure_traits()之前,设置它,例如self.shape.aa = self,然后在_a_fired中调用self.aa._a_fired()。
还有一种方法是aa类中不需要a和b,直接用Options嵌入到aa的视图中。这种组件方法书中有详细介绍的。
2011年08月01日 星期一 14:44
恩,第二种方法最早试的,但是两个界面如何传递数值呢,比如我想把Triangle中d的值同步的传给aa中的c进行显示
# -*- coding: utf-8 -*-
from enthought.traits.api import HasTraits, List, Button, File,Int, Str,Float, Instance
from enthought.traits.ui.api import Group, Handler, View, Item,HGroup, CheckListEditor,VGroup
from enthought.chaco.api import Plot, ArrayPlotData, VPlotContainer
from enthought.traits.ui.menu import Action, ActionGroup, Menu, MenuBar
from enthought.enable.component_editor import ComponentEditor
import numpy as np
class Point(HasTraits):
x = Int
y = Int
view = View(HGroup(Item("x"), Item("y")))
class Shape(HasTraits):
#info = Str #{1}
pass
class Triangle(Shape):
a = Instance(Point, ()) #{3}
b = Instance(Point, ())
c = Instance(Point, ())
d = Str()
view = View(
VGroup(
Item("a", style="custom"), #{4}
Item("b", style="custom"),
Item("c", style="custom"),
Item("d", style="custom"),
)
)
class aa(HasTraits):
shape = Instance(HasTraits)
plot_show = Instance(VPlotContainer)
plot = Instance(VPlotContainer)
a = Button()
b = List( editor = CheckListEditor( values = [ '1', '2', '3', '4' ], cols = 4 ) )
c = Str()
option_menu = Menu(
ActionGroup(
Action(id="open", name=u"demo", action="open_file"),
),
ActionGroup(
Action(id="exit_app", name=u"退出", action="exit_app"),
),
name = u"设置"
)
view =View(
VGroup(
Item('plot_show',editor=ComponentEditor(), show_label=False,width = 450,height = 550),
Item( 'b', style = 'simple', label = u'demo' ),
Item('a'),
Item('c')
),
menubar = MenuBar(option_menu),
resizable = False,width = 600,title = u'demo'
)
def __init__(self):
x = np.linspace(-14, 14, 100)
y = np.sin(x) * x**3 / 1000
plotdata = ArrayPlotData(x=x, y=y)
self.p1 = Plot(plotdata, padding=30, )
self.p1.title = "Xx"
self.p1.plot(("x", "y"), type="line", color="green", bgcolor = "black",line_width=2.0,name = 'G:Xx')
self.p2 = Plot(plotdata, padding=30)
self.p2.title = "Xz"
self.p2.plot(("x", "y"), type="line", color="green", bgcolor = "black",line_width=2.0,name = 'G:Xz')
self.c1 = VPlotContainer(self.p2,self.p1,)
self.plot = self.c1
#self.plot_show = self.plot
def open_file(self):
#self.shape = Options()
self.shape = Triangle()
self.c = Triangle().d
self.plot_show = self.plot
#self.shape.aa = self
self.shape.configure_traits()
def _a_fired(self):
print '2'
xx = np.random.random((5,))
yy = np.random.random((5,))
plotdata = ArrayPlotData(x=xx, y=yy)
self.p1 = Plot(plotdata, padding=40)
self.p2 = Plot(plotdata, padding=40)
self.p1.title = "X"
self.p2.title = "Y"
self.p1.plot(("x", "y"), type="line", color="red",line_width=1.0)
self.p2.plot(("x", "y"), type="line", color="red",line_width=1.0)
c1 = VPlotContainer(self.p2,self.p1)
self.plot = c1
self.smooth_check = False
self.plot_show = self.plot
class Options(aa):
view = View(
Group(
Item( 'b', style = 'simple', label = u'demo' ),
Item('a',label = u'demo',show_label=False),
label = u'demo', show_border = True,
),
)
def __init__(self):
#self.shape.aa = self
pass
def _a_fired(self):
print '1'
self.aa._a_fired()
if __name__ == "__main__":
a = aa()
a.configure_traits()
2011年08月01日 星期一 15:09
你贴的这个程序并不是第二种方法啊。试试下面的程序:
MainWindow中有一个panel属性,然后在MainWindow的view中直接插入panel。事件响应也是响应panel.button。这样用panel.configure_traits()弹出panel的对话框时,也可以用对话框控制MainWindow。
# -*- coding: utf-8 -*-
from enthought.traits.api import HasTraits, List, Button, Instance, on_trait_change
from enthought.traits.ui.api import View, Item,CheckListEditor,VGroup
from enthought.chaco.api import Plot, ArrayPlotData, VPlotContainer
from enthought.traits.ui.menu import Action, ActionGroup, Menu, MenuBar
from enthought.enable.component_editor import ComponentEditor
import numpy as np
class Panel(HasTraits):
button = Button()
choice = List( editor = CheckListEditor( values = [ '1', '2', '3', '4' ], cols = 4 ) )
view = View(VGroup("button","choice"))
class MainWindow(HasTraits):
panel = Instance(Panel)
plot_show = Instance(VPlotContainer)
plot = Instance(VPlotContainer)
option_menu = Menu(
ActionGroup(
Action(id="open", name=u"demo", action="open_file"),
),
ActionGroup(
Action(id="exit_app", name=u"退出", action="exit_app"),
),
name = u"设置"
)
view =View(
VGroup(
Item('plot_show',editor=ComponentEditor(), width = 450,height = 550),
Item('panel', style="custom"),
show_labels=False
),
menubar = MenuBar(option_menu),
resizable = False,width = 600,title = u'demo'
)
def __init__(self, **traits):
super(MainWindow, self).__init__(**traits)
x = np.linspace(-14, 14, 100)
y = np.sin(x) * x**3 / 1000
plotdata = ArrayPlotData(x=x, y=y)
self.p1 = Plot(plotdata, padding=30, )
self.p1.title = "Xx"
self.p1.plot(("x", "y"), type="line", color="green", bgcolor = "black",line_width=2.0,name = 'G:Xx')
self.p2 = Plot(plotdata, padding=30)
self.p2.title = "Xz"
self.p2.plot(("x", "y"), type="line", color="green", bgcolor = "black",line_width=2.0,name = 'G:Xz')
self.c1 = VPlotContainer(self.p2,self.p1,)
self.plot = self.c1
self.panel = Panel()
def open_file(self):
self.panel.configure_traits()
@on_trait_change("panel.button")
def do_plot(self):
xx = np.random.random((5,))
yy = np.random.random((5,))
plotdata = ArrayPlotData(x=xx, y=yy)
self.p1 = Plot(plotdata, padding=40)
self.p2 = Plot(plotdata, padding=40)
self.p1.title = "X"
self.p2.title = "Y"
self.p1.plot(("x", "y"), type="line", color="red",line_width=1.0)
self.p2.plot(("x", "y"), type="line", color="red",line_width=1.0)
c1 = VPlotContainer(self.p2,self.p1)
self.plot = c1
self.smooth_check = False
self.plot_show = self.plot
if __name__ == "__main__":
win = MainWindow()
win.configure_traits()
2011年08月01日 星期一 15:22
恩,这个谢谢了。
我是想在MainWindow中我不想插入Panel 或者我只想要个Panel上的 choice 。
例如我把 MainWindow中的 Item('panel', style="custom"),改为Item('choice', style="custom"),
之前在 MainWindow也有定义
choice = List( editor = CheckListEditor( values = [ '1', '2', '3', '4' ], cols = 4 ) )
我只想在菜单栏中点击设置-demo时弹出Panel ,然后想让panel中的choice和mainwindow中的choice同步变化,怎么去变化
2011年08月01日 星期一 15:32
不在MainWindow中插入Panel的话,那就不要在MainWindow的View中插入它既可。如果想要一个choice的话,那就把panel对应的Item改为:
Item('object.panel.choice', editor = CheckListEditor( values = [ '1', '2', '3', '4' ], cols = 4 ))
如果你要在MainWindow中再定义一个choice属性的话,那么就需要在MainWindow.choice和Mainwindow.panel.choice之间同步,可以用代理,可以用Trait的同步功能。
2011年08月01日 星期一 16:26
@on_trait_change("panel.button")
点击设置-demo 显示界面的时候就执行了一次按钮事件,除了加个if条件判断来避免他一显示就执行,还
有其他方法吗
在用 http://code.enthought.com/projects/traits/docs/html/TUIUG/themes.html#id4
这个例子的theme时,提示
from enthought.traits.ui.wx.themed_text_editor import ThemedTextEditor
File "C:\Python26\lib\site-packages\traitsbackendwx-3.5.0-py2.6.egg\enthought\traits\ui\wx\themed_text_editor.py", line 44, in
from image_slice \
ImportError: cannot import name default_image_slice
2011年08月01日 星期一 20:53
用@on_trait_change("panel:button")
theme我没有用过。
Zeuux © 2024
京ICP备05028076号