Vertical text in Tkinter Canvas

后端 未结 3 1442
梦毁少年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:52

    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)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-21 02:02

    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

    0 讨论(0)
提交回复
热议问题