wx position of items in list

M

M. Clift

Hi All,

This would seem to be easy, but I have had no luck in working out how to do
this.

In wx.python a list of text is displayed as a bitmap and given it's
position.

shape.pos = (145, 270)

What I want to do is to increase the y value for every 20 items in the list,
so the text is displayed to a new line. So for items in list numbered 0 -19
y = 270, for items in list numbered 20 - 39 y = 370 etc...

Thanks for any help,

Malcolm
 
A

Alex Martelli

M. Clift said:
Hi All,

This would seem to be easy, but I have had no luck in working out how to do
this.

In wx.python a list of text is displayed as a bitmap and given it's
position.

shape.pos = (145, 270)

What I want to do is to increase the y value for every 20 items in the list,
so the text is displayed to a new line. So for items in list numbered 0 -19
y = 270, for items in list numbered 20 - 39 y = 370 etc...

maybe 270 + (i//20)*100 is what you want?


Alex
 
M

M. Clift

Hi Alex,

Thanks for helping me again. I'm using the DragImage demo in wx. Here is
some of the code. I am getting the following error regardless of whether I
use a list or string, 'unsupported operand type(s) for //: 'str' and
'int''. I've also tried inserting \n into the text, but I can't get that to
work either.


# Make a shape from some text
text = ['a', 'b','c','d','z','y','a', 'b','c','d','z','y','a',
'b','c','d','z','y','a', 'b','c','d','z','y']
text4 = " "+"".join(text)
bg_colour = wx.Colour(57, 115, 57) # matches the bg image
font4 = wx.Font(36, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Arial")
textExtent = self.GetFullTextExtent(text4, font4)

# create a bitmap the same size as our text
bmp4 = wx.EmptyBitmap(textExtent[0], textExtent[1])

# 'draw' the text onto the bitmap
dc = wx.MemoryDC()
dc.SelectObject(bmp4)
dc.SetBackground(wx.Brush(bg_colour, wx.SOLID))
dc.Clear()
dc.SetTextForeground(wx.BLACK)
dc.SetFont(font4)
dc.DrawText(text4, 0, 0)
dc.SelectObject(wx.NullBitmap)
mask4 = wx.Mask(bmp4, bg_colour)
bmp4.SetMask(mask4)
shape = DragShape(bmp4)
shape.text = "Some dragging text"

for i in text:
shape.pos = (145, 170 + (i//2)*100)
self.shapes.append(shape)

Thanks for your time,

Malcolm
 
C

Cliff Wells

I am getting the following error regardless of whether I
use a list or string, 'unsupported operand type(s) for //: 'str' and
'int''.

That's because you're trying to divide i (a string) by an integer.
for i in text:
shape.pos = (145, 170 + (i//2)*100)
self.shapes.append(shape)

Try this instead:

for i in range(len(text)):
shape.pos = (145, 170 + (i//2)*100)
self.shapes.append(shape)

For future reference, try adding print statements when you get errors
like this so you can verify that you are doing what you think you are.
I'm assuming that you know you can't divide a string by an integer, so
my guess is that for some reason you thought that 'for i in text' would
give you a list of integers. A quick 'print i' after the for statement
would have told you otherwise (as would have the Python tutorial had you
bothered to read it).

Regards,
Cliff
 
M

M. Clift

Hi Cliff,

Thankyou for your help. I know that you can't divide a string by an integer
and true to form I didn't check with 'print' to see what was going on.
Yesterday I tried len, but without the range attached...

Looking at demos and examples it is easy (most of the time) to see how
things work. It is another thing to be creative and cook up some code (even
the most basic) when you are trying to run before you can walk. I do need to
just play around with loads of basic stuff, but like a child in a sweet shop
I get carried away. Saying that I do think that it is sometimes easier to
learn by applying all the help I can get to problems for my project. Getting
something to work for some problem of your own is less dry than some
tutorial example and seems to spot-light.the language.

But before I go to look up some more tutorials I would like to ask something
else
regarding this problem. I can see how this works, but what it does is to
move the
whole text to the new position. Basically I was hoping for a simplified word
wrap.
This is the code (slightly altered) from the wx DragImage demo that I'm
using.

# Make a shape from some text
l1 =
['a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d',]
text = " "+"".join(l1)
bg_colour = wx.Colour(57, 115, 57) # matches the bg image
font = wx.Font(36, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Arial")
textExtent = self.GetFullTextExtent(text, font)

# create a bitmap the same size as our text
bmp = wx.EmptyBitmap(textExtent[0], textExtent[1])

# 'draw' the text onto the bitmap
dc = wx.MemoryDC()
dc.SelectObject(bmp)
dc.SetBackground(wx.Brush(bg_colour, wx.SOLID))
dc.Clear()
dc.SetTextForeground(wx.RED)
dc.SetFont(font)
dc.DrawText(text, 0, 0)
dc.SelectObject(wx.NullBitmap)
mask = wx.Mask(bmp, bg_colour)
bmp.SetMask(mask)
shape = DragShape(bmp)
for i in range(len(text)):
shape.pos = (145, 170 + (i//12)*100)
shape.text = "Some dragging text"
self.shapes.append(shape)

I think that I need to have something further back in the code, before,
the text is drawn. I've tried \ n in the text, but I can't get that to
work.
Any ideas?

Thanks for your time and patience,

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,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top