How to do it...

The text item will be initially displayed using the canvas.create_text() method, with some additional options to use a Consolas font and a blue color.

The dynamic behavior of the text item will be implemented using StringVar. By tracing this Tkinter variable, we can modify the contents of the item:

import tkinter as tk

class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Canvas text items")
self.geometry("300x100")

self.var = tk.StringVar()
self.entry = tk.Entry(self, textvariable=self.var)
self.canvas = tk.Canvas(self, bg="white")

self.entry.pack(pady=5)
self.canvas.pack()
self.update()

w, h = self.canvas.winfo_width(), self.canvas.winfo_height()
options = { "font": "courier", "fill": "blue",
"activefill": "red" }
self.text_id = self.canvas.create_text((w/2, h/2), **options)
self.var.trace("w", self.write_text)

def write_text(self, *args):
self.canvas.itemconfig(self.text_id, text=self.var.get())

if __name__ == "__main__":
app = App()
app.mainloop()

You can try out this program by typing some arbitrary text on the entry input and noticing how it automatically updates the text on the canvas.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset