range and wx co - ords

M

Malcolm Clift

Hi All,

Using wx, I'm trying to draw all items in a range of 50 in a text at
certain co -ordinates (a simply textwrap) . A while back Alex Martelli
was kind enough to give me the following to work with ;

for i in range(len(text)):
dc.DrawText(text, 100, 100 + ( i // 50 *200)

I have slightly changed this to control the spacing between the
letters by adding this;

dc.DrawText(text, 100 + ( i * 12 ), 100 + (i//50)*200)

This starts the new line at the right y co-ordinate, but the x remains
at the position where the last line finished. Would anyone know how to
start the line from the starting x co - ords?


I have had an idea to do it another way, but need some help with this
also.

for i in range(len(text), 50)):
spacer = 200
dc.DrawText(text, 100 + ( i * 12 ), 100 + spacer )

This of course just draws the items at the every 50 mark, as you might
gather, what I want to say is that for every 50 items draw them at
these co - ordinates then go on to the next 50.



Thanks for any help,

Malcolm
 
M

Malcolm Clift

Hi All,

I have now got something approaching what I need. Could someone please
show me how to define the 'line', so rather than just the first 50
items being printed over and over it goes through the text
incrementally?


line = text1[:50]
for i in range(1000):
dc.DrawText(line, 100, 100 + (i//2)*100)


Thanks,

Malcolm
 
B

Bengt Richter

Hi All,

I have now got something approaching what I need. Could someone please
show me how to define the 'line', so rather than just the first 50
items being printed over and over it goes through the text
incrementally?


line = text1[:50]
for i in range(1000):
dc.DrawText(line, 100, 100 + (i//2)*100)
I don't think you want to do 1000 DrawText calls to display 50 characters ;-)

I can't tell what you really want to do, but if you just want to spread characters
evenly to make an array of characters with 50 per row of some width and a box of
some height, I would just walk through the characters and bump coordinates as I went.

If you are tyring to spread _words_ into the space, you might want to keep the characters
in words more together than what overall average spacing will give. I.e., insert extra
space between words, but none or some mild effect between characters. This also depends
on what font you are using, which gets into font metrics. Also how persnickety you are
about precise inclusion within the box area of characters on the right. An '!' is going
to be narrower than an 'M'. Also, do you want to ignore word boundaries and likely break
words between lines?

If you just want to walk through 20 lines of 50 characters taken successively from text1,
you can write something like (untested!!)

cpl = 50 # chars per line
totlines = 20
top = 100; left = 100 # top left pixel position
width = 500; height = 400 # box pixel(?) dimensions
cleantext = ' '.join(text1.split()) # change all whitespace to single spaces??
for linestart in xrange(0, cpl*totlines, cpl):
line = cleantext[linestart:linestart+cpl]
for ix, c in enumerate(line):
# compute position
iy = linestart//cpl # might want to add 1 if top is not baseline pos
x = int(left + width*ix/float(cpl))
y = int(top + height*iy/float(totlines))
dc.DrawText(c, x, y)

But this is not normal word flowing to fit _words_ in a box justified to both sides,
if that's what you really want. If you want that, you should probably output words
whose characters are not separated as much, and put extra space between words, and
not break words at line ends. Also, if your text has real newlines ('\n') or tabs
etc. in it, you may want to change them to spaces (but not necessarily a single space
for each separation as I did in cleantext above).

HTH and that the bug is not too bad (untested, there's bound to be one ;-)

Regards,
Bengt Richter
 
M

Malcolm Clift

Hi Bengt,

Really, a big thankyou for that. It's a lot of work and I appreciate
it. It would seem to be perfect, at very least for what I want to do
right now. I take your point about 'true' word wrap and I may
experiment with that in the future. At least with such a full
explanation I have something to work with if I do.

All the best and thanks once again,

Malcolm
 
M

Malcolm Clift

Hi Bengt,

I thought that I would be able to work out how to do this, but as I'm
new to python it's harder than I thought...

Could you tell me how I would get the text to flow on to a new page?

Thanks,

Malcolm
 
B

Bengt Richter

Hi Bengt,

I thought that I would be able to work out how to do this, but as I'm
new to python it's harder than I thought...

Could you tell me how I would get the text to flow on to a new page?
Maybe, but I'd have to know what you meant by that. E.g., are you showing
multiple pages at once? Waiting for a page-turn click? Do you need to turn
back to previous pages? Once you can spell out _exactly_ what you want to
happen in English, you will probably be able to do it in Python. (And when
you get more fluent in Python, you will likely prefer to spell such things
in Python first ;-)

Regards,
Bengt Richter
 
M

Malcolm Clift

Hi Bengt,

Sorry, I can see how vague my question is...

I am showing multiple pages at once.I suppose that the simplist way to
do it would be to have the position 'left' increase after so many
lines or so many characters. The problem with that is that although
I imagine it is very simple to do so I cannot work out how to say
for every say something like for every 500 characters (or every 5 or
so lines), left = 100 + say 1000.

As I said, at the moment I'm displaying multiple draggable pages (A4
bmps) and I have just started to realise how slow that will be.
Something akin to wading through mud : ) So although it looks nice I
think that I might have to go for a single page display. If that's the
case could you give me some idea as to how I might define 'a new
page', with the option of going forward and backward through the
pages.You've done a lot for me already, so if that's more involved
than I think I would be greatful if you told me where I might start
looking for ways to do this.

Thanks,

Malcolm
 
B

Bengt Richter

Hi Bengt,

Sorry, I can see how vague my question is...

I am showing multiple pages at once.I suppose that the simplist way to
do it would be to have the position 'left' increase after so many
lines or so many characters. The problem with that is that although
I imagine it is very simple to do so I cannot work out how to say
for every say something like for every 500 characters (or every 5 or
so lines), left = 100 + say 1000.
This isn't homework, is it? ;-)
As I said, at the moment I'm displaying multiple draggable pages (A4
bmps) and I have just started to realise how slow that will be.
It is probable that you can improve the speed of dragging bitmaps if
you can do the relevant re-drawing a cached bitmap in a new position
instead of re-creating a bitmap in a new place -- if that's what's
slowing you down.
Something akin to wading through mud : ) So although it looks nice I
think that I might have to go for a single page display. If that's the
case could you give me some idea as to how I might define 'a new
page', with the option of going forward and backward through the
pages.You've done a lot for me already, so if that's more involved
than I think I would be greatful if you told me where I might start
looking for ways to do this.
I would suggest you take an OO aproach, and define a document class
that represents a sequence of pages, which in turn are defined by
a page class. Then you can flesh it out by defining methods for
setting dimensions and positions etc, and for initializing, updating,
and deleting display representations of the pages. Does your page text
come from files? Why don't you post what you have working so far?
I'm sure you will also get comments from people who are more familiar
with wx, which I haven't used. If you post something, I will learn something ;-)

If you don't know about classes and methods and objects and all that,
I suggest reading up on it and trying it out. It is a powerful way
of modeling your problem world, and decomposing it into chunks that
make sense.

Regards,
Bengt Richter
 
M

Malcolm Clift

Hi Bengt,
This isn't homework, is it? ;-)

If it was I wouldn't feel so dumb for not knowing half of this stuff.
No, I'm just a late starter with an idea for a program.

Thankyou for your suggestion of taking the OO approach. I think that
you are right and I need to go away and work this out. It was just
that I was trying to find a simple / basic way just to get the thing
up and running before refining it.

The text will eventually come from a generated file, but at the moment
I haven't hooked up the part that does the generating with the
display. At the moment I'm just throwing a little bit of text in just
above the display part of the code.
Why don't you post what you have working so far?...I will learn
something ;-)

Bengt, I really doubt there is anything I can teach you about python /
wx. It is kind of you to suggest otherwise : ) At the present time I
have basically cobbled together the dragImage demo. The 'page' is just
an A4 bmp. That code came from looking at how the cards are displayed
in the demo. To this I have just added some static text ( the stuff
you've helped me to word wrap). So I end up with a so called draggable
'page' with text. To add more pages I pin the bmps on to an empty bmp
so that they drag as one whole. As I said in the other post, one page
is ok, but adding more slows things down a lot. As this is just a mess
around with the DragImage demo I presume that the bmps are buffered,
but as I write, I think that I may have to look into this, as perhaps
pinning 'pages' onto a wx.EmptyBitmap affect things somehow. That or
that with just a few extra pages the bmp becomes massive.

If you or anyone else is interested I will of course post it.

All the best,

Malcolm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top