Betamoo
Betamoo

Reputation: 15960

Vertical text in Tkinter Canvas

Is there a way to draw vertical text in Tkinter library? (Python recommended)

textID = w1.create_text(5, 5, anchor="nw")
w1.itemconfig(textID, text = "This is some text")

Upvotes: 6

Views: 15651

Answers (3)

Novel
Novel

Reputation: 13729

Since people are being linked to this answer, I'll add an update.

In tcl 8.6, the create_text method got an angle option. You can check your tcl version with Tkinter.TclVersion. If you have 8.6 or greater, you can use:

textID = w1.create_text(5, 5, anchor="nw", angle=90)

Upvotes: 21

Tom Fuller
Tom Fuller

Reputation: 5349

I don't know any way to make vertical text in tkinter, but you can just make an image of the vertical text you want

  1. Screen shot the text you want and crop it
  2. Screen shot the background colour and crop it
  3. In word or powerpoint or something, put the text over the background
  4. Take another screenshot and crop that
  5. Put that screen shot in paint
  6. Save the paint file in the same folder as your program
  7. convert the paint file to a gif online: http://image.online-convert.com/convert-to-gif
  8. Use the following code to put the image in your program

vertical_text = PhotoImage(file = "<your file name>.gif") canvas.create_image(x, y, image = vertical_text)

Here is a screenshot from a tkinter window in a program I'm making which lets you revise words example

Upvotes: 1

user2555451
user2555451

Reputation:

If you are asking whether tkinter.Canvas.create_text has something like this:

textID = w1.create_text(5, 5, anchor="nw", orient=tkinter.VERTICAL)

then the answer is no. The create_text method can only create horizontal text.


However, you can use str.join to create vertical text:

from tkinter import Tk, Canvas
root = Tk()
canvas = Canvas()
canvas.grid()
canvas.create_text((10, 5), text="\n".join("This is some text"), anchor="nw")
root.mainloop()

Example:

enter image description here

While this may not be as elegant as simply setting an option on the create_text method, it does work.

Upvotes: 6

Related Questions