Python Forum
[Tkinter] How to erase previous output from the canvas?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to erase previous output from the canvas?
#1
I'm tinkering with tkinter. I have this to make a window. It takes an input and displays the input text as output.

def myWindow1():
    window = tkinter.Tk()
    window.title("Get a text input and echo it")
    window.config(bg='light blue')
    window.geometry('640x480')
    # make a frame to take the canvas
    frame=Frame(window,width=400,height=300, bg='lightblue')
    frame.pack(expand=True, fill=BOTH) #.grid(row=0,column=0)

    xscrollbar = Scrollbar(frame, orient=HORIZONTAL, width=20, activebackground='red', )
    yscrollbar = Scrollbar(frame, orient=VERTICAL, width=20, activebackground='green', )
    xscrollbar.pack( side = BOTTOM, fill = X )
    yscrollbar.pack( side = RIGHT, fill = Y )

    #canvas1 = tkinter.Canvas(window, xscrollcommand = xscrollbar.set, yscrollcommand = yscrollbar.set, bg='white', width = 400, height = 300,  relief = 'raised')
    #canvas1.pack()
    canvas1 = tkinter.Canvas(frame, bg='white', width = 400, height = 300,  relief = 'raised')
    canvas1.config(xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
    canvas1.pack(padx=10, pady=10)

    label1 = tkinter.Label(frame, text='Echo a text input')
    label1.config(font=('helvetica', 14))
    canvas1.create_window(200, 25, window=label1)

    label2 = tkinter.Label(frame, text='Type your text:')
    label2.config(font=('helvetica', 10))
    canvas1.create_window(200, 100, window=label2)

    entry1 = tkinter.Entry (frame) 
    canvas1.create_window(200, 140, window=entry1)

    def getInput():
        
        msg = entry1.get()
        
        label3 = tkinter.Label(frame, text= 'You entered this text:',font=('helvetica', 10))
        canvas1.create_window(200, 210, window=label3)
        # wraplength is in pixels not characters!!
        #label4 = tkinter.Label(window, text=msg,font=('helvetica', 10, 'bold'), anchor='w', width=35, wraplength=80)
        #label4 = tkinter.Label(frame, text=msg,font=('helvetica', 10, 'bold'), anchor='w', width=55, wraplength=390)
        label4 = tkinter.Label(frame, text=msg,font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
        #canvas1.update()
        canvas1.create_window(200, 250, window=label4)
        # don't set the y value too high or you won't see the output!
        #canvas1.create_window(200, 320, window=label4)
        
    button1 = tkinter.Button(text='Get the text message', command=getInput, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
    canvas1.create_window(200, 180, window=button1)

    window.mainloop()
For example I enter "Hello world!" I see "Hello world!" as output.

Then I enter "Hi me!" I see 'Hi me!" as output, centred over the previous output, "Hello world!".

How do I erase the previous output before the next output gets written? I tried canvas1.update() but that did not do it.
Reply


Messages In This Thread
How to erase previous output from the canvas? - by Pedroski55 - Jul-03-2020, 11:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,159 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] output to canvas widget freakbot 2 9,971 Dec-01-2016, 12:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020