Jul-03-2020, 11:34 PM
I'm tinkering with tkinter. I have this to make a window. It takes an input and displays the input text 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.
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.