[Tutor] Placement of text on a canvas
Rick Pasotto
rick@niof.net
Sat Mar 29 16:01:01 2003
On Fri, Mar 28, 2003 at 10:00:19PM -0500, Rick Pasotto wrote:
> On Thu, Mar 06, 2003 at 02:33:13PM -0600, Henry Steigerwaldt wrote:
> >
> > Below is the line of code I am using:
> >
> > canvas.create_text(100,342, text='P ' + '10/20/40/40', fill='red',
> > font=('verdana', 8, 'bold'))
> >
> > The "P" stands for the Probability of Precipitation, and the numbers
> > are the probabilities for different periods of time. They can vary
> > from single digits (2, 5 etc.) to 100, hence the varying line length,
> > even when no additional numbers are added to the line.
> >
> > What can I do to keep the left side (start of numbers) AT THE SAME X,Y
> > LOCATION from changing? Is there an option that would work?
>
> You need to add the option 'anchor=W'. Thus:
>
> canvas.create_text(30,30,text="whatever",fill='red',anchor=W)
>
> You are deleting the first text item aren't you?
Investigating a little more I've learned better how this works. Turns
out that the x,y coordinates supplied to create_text refer to the anchor
point and the anchor point defaults to 'center'. You can test this by
creating a default canvas and adding a line with known coords and then
using those coords to add text using different anchors.
cv = Canvas(background='white')
cv.pack()
cv.create_line(100,50,300,50)
cv.create_text(100,50,anchor=CENTER,fill='red',
text='this is a line of text')
cv.create_text(100,50,anchor=NW,fill='blue',
text='this is a line of text')
cv.create_text(100,50,anchor=SW,fill='green',
text='this is a line of text')
The blue text will be below the line and the green will be above the
line but both will line up on the left with the endpoint of the line.
The center of the red text will be the left endpoint of the line and
the line will go through the center of the text.
--
"As empty vessels make the loudest sound, so they that have
the least wit are the greatest blabbers." -- Plato
Rick Pasotto rick@niof.net http://www.niof.net