noobprogrammer
noobprogrammer

Reputation: 153

How do I delete existing text in Tkinter?

I’m trying to make a function that changes slides in an animated presentation by using if statements, but when the slide is supposed to change, the existing widget stays. Here is my minimal reproducible example (by the way, the ‘description.insert’ is formatted correctly on my computer but not on my phone):

from tkinter import *
import random
import time
tk = Tk()
canvas = Canvas(tk, width = 400, height = 400)
tk.title('Diffusion')
canvas.pack()
 
slide = 0
 
def changeSlide():
    global slide
    slide += 1
    #WRITE TEXT
    if slide == 1:
        description = Text(tk, bd=0, height=5, width = 50)
        description.insert(INSERT, 'Diffusion is the net movement of particles from an area of higher concentration to an area of lower concentration, which results in the concentration being even. Here is an example, with the particles represented by orange dots:')
        description.place(x=0, y=190)
    elif slide == 2:
        #DRAW PARTICLES  
        particle = canvas.create_oval(10, 10, 20, 20, fill = 'orange')
 
nexT = Button(tk, text = 'NEXT', command = changeSlide)
nexT.pack()
nexT.place(bordermode = 'inside', x = 350, y = 375)

Upvotes: 0

Views: 55

Answers (1)

martineau
martineau

Reputation: 123393

Although it sounds like you may have figured out a way to solve the problem, here's another, possibly better, approach. It creates a separate tk.Frame named slide to hold the current slide's contents. Doing so makes it relatively easy to iterate over (just) its contents — the "children" widgets it contains — and destroy them.

Note how an attribute has been added to the slide frame named num to indicate what slide was last displayed. The advantage of doing like this is now you don't have declare it global in the slide_changer() function to access and change its value because now it's part of the slide frame itself.

Also note the use of the wrap=tk.WORD keyword argument when creating the tk.Text widget for the description which should force the text inserted into it to be formatted properly on all your devices. This widget option is described in some Tkinter documentation about the Text widget and the various options it supports.

import tkinter as tk
import time
import random

NUM_SLIDES = 2

root = tk.Tk()
root.title('Diffusion')

slide = tk.Frame(root, width=400, height=400)
slide.num = 0  # Add attribute to track slide being displayed.
slide.pack()

next_btn = tk.Button(root, text='NEXT')
next_btn.place(bordermode='inside', x=350, y=375)

def slide_changer():
    # Clear slide frame.
    for child in slide.winfo_children():
        child.destroy()

    slide.num += 1
    if slide.num > NUM_SLIDES:
        slide.num = 1  # Repeat starting at beginning.

    if slide.num == 1:  # Write text.
        description = tk.Text(slide, bd=0, height=5, width=50, wrap=tk.WORD)
        description.insert(tk.INSERT,
            'Diffusion is the net movement of particles from an area of higher '
            'concentration to an area of lower concentration, which results in '
            'the concentration being even. Here is an example, with the '
            'particles represented by orange dots:')
        description.place(x=0, y=190)

    elif slide.num == 2:  # Draw particles.
        canvas = tk.Canvas(slide, width=400, height=400)
        canvas.pack()
        particle = canvas.create_oval(10, 10, 20, 20, fill='orange')

next_btn.config(command=slide_changer)

root.mainloop()

Upvotes: 1

Related Questions