简体   繁体   中英

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")

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)

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:

在此处输入图片说明

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

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 例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM