Vertical text in Tkinter Canvas

后端 未结 3 1447
梦毁少年i
梦毁少年i 2020-12-21 01:43

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 = \"Thi         


        
3条回答
  •  天命终不由人
    2020-12-21 01:54

    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.

提交回复
热议问题