Hi thanks for your reply. I mean the wx.aui.
please look my code i'm trying to embedd the trend class is a plot
example i've found using floatcanvas. It works but really small. I did
the same trend with buffered cambas and it works well. I think is
something related to the container's resize event but i'm trying with
no results. Im trying all this under Windows Python 2.6 and wxPython
2.8.
Thanks
This is the trend class code:
trend.py
#!/usr/bin/env python
import wx
import numpy as N
import sys
sys.path.append("../")
try:
from floatcanvas import NavCanvas, FloatCanvas, Resources
except ImportError: # if it's not there locally, try the wxPython lib.
from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
class TREND(wx.Window):
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, mgr=None):
wx.Window.__init__(self, parent, id, pos, size, wx.NO_BORDER)
## Set up the MenuBar
"""
MenuBar = wx.MenuBar()
file_menu = wx.Menu()
item = file_menu.Append(wx.ID_ANY, "E&xit","Terminate the
program")
self.Bind(wx.EVT_MENU, self.OnQuit, item)
MenuBar.Append(file_menu, "&File")
draw_menu = wx.Menu()
item = draw_menu.Append(wx.ID_ANY, "&Run","Run the test")
self.Bind(wx.EVT_MENU, self.RunTest, item)
item = draw_menu.Append(wx.ID_ANY, "&Stop","Stop the test")
self.Bind(wx.EVT_MENU, self.Stop, item)
MenuBar.Append(draw_menu, "&Plot")
help_menu = wx.Menu()
item = help_menu.Append(wx.ID_ANY, "&About",
"More information About this program")
self.Bind(wx.EVT_MENU, self.OnAbout, item)
MenuBar.Append(help_menu, "&Help")
self.SetMenuBar(MenuBar)
"""
#self.CreateStatusBar()
#self.SetStatusText("")
wx.EVT_CLOSE(self, self.OnCloseWindow)
# Add the Canvas
NC = NavCanvas.NavCanvas(self , wx.ID_ANY ,(200,500),
ProjectionFun = None,
Debug = 0,
BackgroundColor = "WHITE"
)
self.Canvas = NC.Canvas
self.Canvas.NumBetweenBlits = 1000
#self.AutoLayout()
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self, 1, wx.EXPAND)
self.SetSizer(sizer)
#sizer.Fit(self)
#self.Layout()
#self.Fit()
# Add a couple of tools to the Canvas Toolbar
wx.EVT_SIZE(self, self.OnRESIZE)
tb = NC.ToolBar
tb.AddSeparator()
StopButton = wx.Button(tb, wx.ID_ANY, "Stop")
tb.AddControl(StopButton)
StopButton.Bind(wx.EVT_BUTTON, self.Stop)
PlayButton = wx.Button(tb, wx.ID_ANY, "Run")
tb.AddControl(PlayButton)
PlayButton.Bind(wx.EVT_BUTTON, self.RunTest)
tb.Realize()
self.Show(True)
self.timer = None
self.DrawAxis()
return None
def OnRESIZE(self, event):
self.Layout()
def OnAbout(self, event):
dlg = wx.MessageDialog(self, "This is a small program to
demonstrate\n"
"the use of the FloatCanvas\n"
"for simple plotting",
"About Me", wx.OK |
wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def ZoomToFit(self,event):
self.Canvas.ZoomToBB()
def OnQuit(self,event):
self.Close(True)
def OnCloseWindow(self, event):
self.Destroy()
def DrawAxis(self):
Canvas = self.Canvas
# Draw the Axis
# Note: the AddRectangle Parameters all have sensible
# defaults. I've put them all here explicitly, so you can see
# what the options are.
#2*N.pi
self.Canvas.AddRectangle((0,0),
(3, 6),
LineColor = "Black",
LineStyle = "Solid",
LineWidth = 1,
FillColor = None,
FillStyle = "Solid",
InForeground = 0)
#Grilla
lp = N.random.rand(2,2)*0
lp[0,0] = 0.75
lp[1,0] = 0.75
lp[1,1] = 6.00
self.Canvas.AddLine(lp,
LineColor = "Gray",
LineStyle = "Solid",
LineWidth = 1,
InForeground = 1)
lp[0,0] = 1.50
lp[1,0] = 1.50
lp[1,1] = 6.00
self.Canvas.AddLine(lp,
LineColor = "Gray",
LineStyle = "Solid",
LineWidth = 1,
InForeground = 1)
lp[0,0] = 2.25
lp[1,0] = 2.25
lp[1,1] = 6.00
self.Canvas.AddLine(lp,
LineColor = "Gray",
LineStyle = "Solid",
LineWidth = 1,
InForeground = 1)
for i in range(1, 6):
lp[0,0] = 0.00
lp[0,1] = i
lp[1,0] = 3.00
lp[1,1] = i
self.Canvas.AddLine(lp,
LineColor = "Gray",
LineStyle = "Solid",
LineWidth = 1,
InForeground = 1)
""" Texto en Axis
for tic in N.arange(7):
self.Canvas.AddText("%1.1f"%tic,
(tic,-1.1),
Position = 'tc')
for tic in N.arange(-1, 1.1, 0.5):
self.Canvas.AddText("%1.1f"%tic,
(0,tic),
Position = 'cr')
"""
# Add a phantom rectangle to get the bounding box right
# (the bounding box doesn't get unscaled text right)
# No se si necesito esto
#self.Canvas.AddRectangle((-0.7, -1.5), (7, 3), LineColor =
"BLUE")
Canvas.ZoomToBB()
Canvas.Draw()
def Stop(self,event):
if self.timer:
self.timer.Stop()
def OnTimer(self,event):
self.count += .1
self.data1[:,1] = N.sin(self.time+self.count) #fake move
#rmdi
temp = self.data1[0]
#self.data2[0] = [0, 0]
#self.data2[1] = [100, 100]
tmp = [0,0]
self.data1[0] = tmp #self.data1[1]
tmp = [5,6]
self.data1[1] = tmp #temp
print "########### <<<< >>>>>> ###############"
#print type(self.data1)
#print self.data2
#rmdi
c = N.random.rand(10,1)*3
d = N.random.rand(10,1)*6
e = c + d
a = N.random.rand(100,2)*0.5
tmpi = 0.00
for i in range(0,100):
a[i,1] = tmpi
tmpi = tmpi + 0.06
a[99,1] = 6.00
#####################
b = N.random.rand(100,2)*2
tmpi = 0.00
for i in range(0,100):
b[i,1] = tmpi
tmpi = tmpi + 0.06
b[99,1] = 6.00
#a.ravel()
#a.shape(10,2)
print a
print a.shape
self.line.SetPoints(a)#self.data1)
self.line2.SetPoints(b)#self.data1)
self.Canvas.Draw()
def RunTest(self,event = None):
self.n = 100
self.dT = 0.5
self.time = 2.0*N.pi*N.arange(100)/100.0
self.data1 = 1.0*N.ones((100,2))
self.data1[:,0] = self.time
self.data1[:,1] = N.sin(self.time)
Canvas = self.Canvas
self.Canvas.ClearAll()
self.DrawAxis()
print "^^^^^^^^^^^"
print self.data1
self.line = Canvas.AddLine(self.data1,
LineColor = "Blue",
LineStyle = "Solid",
LineWidth = 1,
InForeground = 1)
self.line2 = Canvas.AddLine(self.data1,
LineColor = "Red",
LineStyle = "Solid",
LineWidth = 1,
InForeground = 1)
self.Canvas.Draw()
self.timerID = wx.NewId()
self.timer = wx.Timer(self,self.timerID)
wx.EVT_TIMER(self,self.timerID,self.OnTimer)
self.count = 0
self.timer.Start(int(self.dT*1000))
###############################################################################3
And this is the AUI frame code:
aui.py
import wx
import wx.aui
from bufferedcanvas import *
import random
import numpy as N
import sys
from TREND import TREND
# Definicion de la clase tendencia
class TendenciaCanvas(BufferedCanvas):
def __init__(self,parent,ID=-1):
BufferedCanvas.__init__(self,parent,ID)
wx.EVT_MOUSEWHEEL(self, self.OnRueda)
self.a = 0
def OnRueda(self, GETO):
self.a = self.a + GETO.GetWheelRotation()
print self.a
def draw(self, dc):
dc.SetBackground(wx.Brush("Black"))
dc.Clear()
# Bloque de codigos para simular grafico de tendencias
x1 = 0
y1 = 0
y2 = 0
x2 = 0
maximo = 300
minimo = 0
width = 300
nDat = 150
fY = 600 / nDat
dc.SetPen(wx.Pen('green', 2))
for i in range(nDat):
valor = random.randint(0,200)
x2 = round(width / (maximo - minimo) * (valor - minimo))
y2 = y2 + fY
dc.DrawLine(x1, y1, x2, y2)
x1 = x2
y1 = y2
# Integrando el metodo para generar graficos de tendencias
x1 = 0
y1 = 0
y2 = 0
x2 = 0
maximo = 300
minimo = 0
width = 300
nDat = 100
fY = 600 / nDat
dc.SetPen(wx.Pen('red', 2))
for i in range(nDat):
valor = random.randint(50,250)
x2 = round(width / (maximo - minimo) * (valor - minimo))
y2 = y2 + fY
dc.DrawLine(x1, y1, x2, y2)
x1 = x2
y1 = y2
# Fin de clase tendencia
class MyFrame(wx.Frame):
def __init__(self, parent, id=-1, title='Tiempo Real',
pos=wx.DefaultPosition, size=(800, 600),
style=wx.DEFAULT_FRAME_STYLE):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self._mgr = wx.aui.AuiManager(self)
#self._mgr.SetManagedWindow(self)
# Anexando un boton
MiBoton = wx.Button(self, label="tendencia")
MiBoton.Bind(wx.EVT_BUTTON, self.ClickMiBoton)
#Sizer = wx.BoxSizer(wx.VERTICAL)
#Sizer.Add(MiBoton, 0, wx.ALIGN_CENTER|wx.ALL, 5)
#self.SetSizerAndFit(Sizer)
# create several text controls
text1 = wx.TextCtrl(self, -1, 'Pane 1 - sample text',
wx.DefaultPosition, wx.Size(200,150),
wx.NO_BORDER | wx.TE_MULTILINE)
text2 = wx.TextCtrl(self, -1, 'Pane 2 - sample text',
wx.DefaultPosition, wx.Size(200,150),
wx.NO_BORDER | wx.TE_MULTILINE)
text3 = wx.TextCtrl(self, -1, 'Pane 3 - sample text',
wx.DefaultPosition, wx.Size(200,150),
wx.NO_BORDER | wx.TE_MULTILINE)
#Sizer = wx.BoxSizer(wx.VERTICAL)
#Sizer.Add(NothingBtn, 0, wx.ALIGN_CENTER|wx.ALL, 5)
#Sizer.Add(MsgBtn, 0, wx.ALIGN_CENTER|wx.ALL, 5)
ctrl = ControlTendencia(self, -1, wx.DefaultPosition, wx.Size
(200, 200))
ctrl21 = TREND(self, -1, wx.DefaultPosition, wx.Size(200,
300))
# add the panes to the manager
self._mgr.AddPane(ctrl21, wx.RIGHT, 'Tendencia en Tiempo')
self._mgr.AddPane(text2, wx.LEFT, 'Numericos')
self._mgr.AddPane(text3, wx.CENTER,'Profundidad')
#self._mgr.AddPane(MiBoton, wx.RIGHT)
self._mgr.AddPane(ctrl, wx.RIGHT,'Tendencia')
# tell the manager to 'commit' all the changes just made
self._mgr.Update()
self.Bind(wx.EVT_CLOSE, self.OnClose)
def ClickMiBoton(self, event):
# Este metodo inicia la tendencia en tiempo
FrameTendencia = TestFrame()
FrameTendencia.Show()
def OnClose(self, event):
# deinitialize the frame manager
self._mgr.UnInit()
# delete the frame
self.Destroy()
# Size Report Control
class SizeReportCtrl(wx.PyControl):
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, mgr=None):
wx.PyControl.__init__(self, parent, id, pos, size,
wx.NO_BORDER)
self._mgr = mgr
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnPaint(self, event):
dc = wx.PaintDC(self)
size = self.GetClientSize()
s = ("Size: %d x %d")%(size.x, size.y)
dc.SetFont(wx.NORMAL_FONT)
w, height = dc.GetTextExtent(s)
height = height + 3
dc.SetBrush(wx.WHITE_BRUSH)
dc.SetPen(wx.WHITE_PEN)
dc.DrawRectangle(0, 0, size.x, size.y)
dc.SetPen(wx.LIGHT_GREY_PEN)
dc.DrawLine(0, 0, size.x, size.y)
dc.DrawLine(0, size.y, size.x, 0)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2))
if self._mgr:
pi = self._mgr.GetPane(self)
s = ("Layer: %d")%pi.dock_layer
w, h = dc.GetTextExtent(s)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2)+
(height*1))
s = ("Dock: %d Row: %d")%(pi.dock_direction, pi.dock_row)
w, h = dc.GetTextExtent(s)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2)+
(height*2))
s = ("Position: %d")%pi.dock_pos
w, h = dc.GetTextExtent(s)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2)+
(height*3))
s = ("Proportion: %d")%pi.dock_proportion
w, h = dc.GetTextExtent(s)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2)+
(height*4))
def OnEraseBackground(self, event):
# intentionally empty
pass
def OnSize(self, event):
self.Refresh()
event.Skip()
class ControlTendencia(wx.PyControl):
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, mgr=None):
wx.PyControl.__init__(self, parent, id, pos, size,
wx.NO_BORDER)
self._mgr = mgr
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.SetBackground(wx.Brush("Black"))
dc.Clear()
size = self.GetClientSize()
s = ("Size: %d x %d")%(size.x, size.y)
# Grafico de tendencia
# Bloque de codigos para simular grafico de tendencias
x1 = 0
y1 = 0
y2 = 0
x2 = 0
maximo = 300
minimo = 0
width = 300
nDat = 150
fY = 600 / nDat
dc.SetPen(wx.Pen('green', 2))
for i in range(nDat):
valor = random.randint(0,200)
x2 = round(width / (maximo - minimo) * (valor - minimo))
y2 = y2 + fY
dc.DrawLine(x1, y1, x2, y2)
x1 = x2
y1 = y2
# Integrando el metodo para generar graficos de tendencias
x1 = 0
y1 = 0
y2 = 0
x2 = 0
maximo = 300
minimo = 0
width = 300
nDat = 100
fY = 600 / nDat
dc.SetPen(wx.Pen('red', 2))
for i in range(nDat):
valor = random.randint(50,250)
x2 = round(width / (maximo - minimo) * (valor - minimo))
y2 = y2 + fY
dc.DrawLine(x1, y1, x2, y2)
x1 = x2
y1 = y2
#
if self._mgr:
pi = self._mgr.GetPane(self)
s = ("Layer: %d")%pi.dock_layer
w, h = dc.GetTextExtent(s)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2)+
(height*1))
s = ("Dock: %d Row: %d")%(pi.dock_direction, pi.dock_row)
w, h = dc.GetTextExtent(s)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2)+
(height*2))
s = ("Position: %d")%pi.dock_pos
w, h = dc.GetTextExtent(s)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2)+
(height*3))
s = ("Proportion: %d")%pi.dock_proportion
w, h = dc.GetTextExtent(s)
dc.DrawText(s, (size.x-w)/2, ((size.y-(height*5))/2)+
(height*4))
def OnEraseBackground(self, event):
# intentionally empty
pass
def OnSize(self, event):
self.Refresh()
event.Skip()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()