Home > Net >  Change canvas text position tkinter
Change canvas text position tkinter

Time:10-23

How do I edit the x and y position of text in a canvas

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=100, height=100, bd=0, highlightthickness=0)
p = canvas.create_text(width/2, height/2, text = '', anchor="center", justify='center', fill="white",  font = ("Arial", int(80/(0.371428571 * size))))

I can use this to edit the text:

canvas.itemconfigure(p, text=currentTime   "\n"   tempDate)
canvas.itemconfigure(p, anchor="center")
canvas.itemconfigure(p, justify='center')

but I cant find out how to configure the values of x and y (which are set when I do (width/2, height/2). I want to be able to change the x and y's to the new center of the canvas when it is increased (so that the text remains in the center) I can do it if I know how to change the x and y

Things like __X or __Y or coords or __coords or x or y don't work in itemconfigure and gave me an error

This is what it should take:

    (method)
create_text(__x: float, __y: float, *, activefill: _Color = ..., activestipple: str = ..., anchor: _Anchor = ..., disabledfill: _Color = ..., disabledstipple: _Bitmap = ..., fill: _Color = ..., font: _FontDescription = ..., justify: Literal['left', 'center', 'right'] = ..., offset: _ScreenUnits = ..., state: Literal['normal', 'active', 'disabled'] = ..., stipple: _Bitmap = ..., tags: _TkinterSequence[str] | str = ..., text: float | str = ..., width: _ScreenUnits = ...) -> _CanvasItemId

CodePudding user response:

You can simply use canvas.moveto(p, x, y) where (x, y) is the position you want.

Or canvas.coords(p, x, y) which preserves the anchor option.

  • Related