Search code examples
pythontkintertk-toolkittkinter-canvas

How to edit text on button click in Tkinter Python?


I want to edit text of function canvas.create_text() in Tkinter. How can I do it?

I tried this code. But it has mistakes.

from tkinter import *

root = Tk()
root.title("Example")
root.geometry("500x500")

canvas = Canvas(root, width=500, height=500).pack()

text = canvas.create_text(0, 0, text="click")

def edit():
   text["text"] = 'clicked'

click_btn = Button(root, text="Click on this", command=edit)
click_btn = canvas.create_window(0, 50, window=click_btn)

root.mainloop()

I don't know what must I will do.


Solution

  • You need this canvas.itemconfig

    text = canvas.create_text(0, 0, text="click")
    def clicked():
              res = 'clicked'
              canvas.itemconfig(text, text=res)