Discussion:
Setting allowed size of text object on canvas in Tkinter
Eric Brunel
2003-06-10 08:31:24 UTC
Permalink
Hi everybody!
How do I force a text object to be of a required size on a canvas in
Tkinter?
I have a Tkinter.Canvas widget onto which I have drawn a rectangle with
create_rectangle(). Inside this rectangle I want to put a text object, but
also make sure that the text does not overflow the rectangle's borders
(rather, the overflow should be cut off from view). Currently I'm trying
to do this with create_text, but that only accepts coordinates for a point
and not for the bounding box. Any ideas?
Is this what you want?

---------------------------------
from Tkinter import *

root = Tk()
c = Canvas(root)
c.pack()
c.create_text(100, 100, text='spam spam spam spam spam', width=75)
root.mainloop()
---------------------------------

For all options for the text item in a cnavas, see:
http://dev.scriptics.com/man/tcl8.4/TkCmd/canvas.htm#M152

HTH
--
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
Mickel Grönroos
2003-06-10 07:18:45 UTC
Permalink
Hi everybody!

How do I force a text object to be of a required size on a canvas in
Tkinter?

I have a Tkinter.Canvas widget onto which I have drawn a rectangle with
create_rectangle(). Inside this rectangle I want to put a text object, but
also make sure that the text does not overflow the rectangle's borders
(rather, the overflow should be cut off from view). Currently I'm trying
to do this with create_text, but that only accepts coordinates for a point
and not for the bounding box. Any ideas?

Cheers,

/Mickel G.

--
Mickel Gr?nroos, application specialist, linguistics, Research support, CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi
Mickel Grönroos
2003-06-10 09:19:59 UTC
Permalink
Post by Eric Brunel
Hi everybody!
How do I force a text object to be of a required size on a canvas in
Tkinter?
[...]
Is this what you want?
---------------------------------
from Tkinter import *
root = Tk()
c = Canvas(root)
c.pack()
c.create_text(100, 100, text='spam spam spam spam spam', width=75)
root.mainloop()
---------------------------------
No, the text is still wrapping. I would like to be able to se a "height"
option too, so that only the text that fits in the area defined by "width"
and "height" would be visible (and the rest simply not shown at all). Do
you know how to ackomplish that?

(Currently, I use bbox() to get the coordinates of the rectangle and the
text, then I chop of characters from the end of my string in a loop until
the bbox of the text is fully overlapped by the rectangle -- ugly and
possibly slow.)

/Mickel

--
Mickel Gr?nroos, application specialist, linguistics, Research support, CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi
Eric Brunel
2003-06-10 12:27:13 UTC
Permalink
Post by Mickel Grönroos
No, the text is still wrapping. I would like to be able to se a "height"
option too, so that only the text that fits in the area defined by "width"
and "height" would be visible (and the rest simply not shown at all). Do
you know how to ackomplish that?
OK, sorry, I misunderstood you. I don't know of any means to achieve that
simply, apart from using a canvas window with an inactive Text widget in it:

-------------------------------
from Tkinter import *

root = Tk()
c = Canvas(root)
c.pack()
t = Text(c, width=75, height=24, relief=FLAT, bd=0)
t.insert(1.0, 'spam spam spam spam spam\nspam spam spam and spam')
t.configure(state=DISABLED)
c.create_window(100, 100, window=t, width=75, height=24, anchor=NW)
c.create_rectangle(98, 98, 177, 126)
root.mainloop()
-------------------------------

However, you'll get the basic behaviour of the Text widget, which you may not
need (e.g., pressing the mouse in the text and dragging selects the text and
makes it scroll...)

HTH
--
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
Loading...