Home > database >  Change Aspect ratio of tkinter canvas text object
Change Aspect ratio of tkinter canvas text object

Time:10-16

I have placed some text onto a tkinter canvas.

How would I go about changing the aspect ratio of this text.

In my case I have a single letter on a canvas created with canvas.create_text. I would like to change the height of this letter while retaining its width. Visually it would look like the letter is falling forward or rotation about the horizon. I also would like to change the width of the letter without changing its height. Is this case it would visually look like it is rotating.

canvas.scale changes the coordinate system where the letter is drawn but not the scale of the letter it self.

Here is a short example. I want the height of the letter to follow the height of the lines on either side. But the width of the letter should remain the same.

import tkinter as tk
from tkinter import font


class FlipDigit():
    def __init__(self,master):
        self.master = master
        self.digit = tk.Canvas(master,width= 150,height = 150,bg='black')
        self.font = font.Font(family ='Calibri',size = 150)
        self.letter = self.digit.create_text(75,75,text = 'A',fill = 'white', font = self.font)
        self.digit.create_line(5,25,5,125,width=10,fill = '#808080')
        self.digit.create_line(145,25,145,125,width = 10,fill = '#808080')
        self.digit.pack()        
        self.count = 0
        self.master.after(50,self.Bump)
    def Bump(self):
        yscale = .8
        if self.count > 10:
            yscale = 1.25
        if self.count >20:
            self.count = -1
        self.count  = 1
        self.digit.scale('all',75,75,1,yscale)
        self.master.after(150,self.Bump)

if __name__ == '__main__':
    root = tk.Tk()
    Digit = FlipDigit(root)
    root.mainloop()

CodePudding user response:

I don't think there is any way to do that in tkinter. You cannot change the height of a font independently from the width.

  • Related