How to delete 1 label when adding another - python tkinter

10.5k views Asked by At

Fairly new to python. I am using option menus and I have labels attached to them:

from tkinter import *
root=Tk()

def f(s):
    if s=="btn":
        one=Label(root,text="one blah blah")
        one.grid(column=1,row=2)
    if s=="btn2":
        two=Label(root,text="two")
        two.grid(column=1,row=2)


v=StringVar(root)
v.set("f")
a=OptionMenu(root,v,"btn","btn2",command=f)
a.grid(column=1,row=1)

root.configure()
root.geometry("100x100")
root.mainloop()

I can't figure out how to make the "one blah blah" to delete the "two" so that it isn't visible when you press btn2 after pressing btn1. I have tried .grid_forget and similar stuff but I can never get it to work.

If it matters this is an example program for a larger program I am creating where there are many different option menus and labels.

2

There are 2 answers

0
furas On BEST ANSWER

You can create empty label at start and later change only text in this label

import tkinter as tk

# --- functions ---

def f(s):
    if s == "btn":
        l['text'] = "one blah blah"
    elif s == "btn2":
        l['text'] = "two"
    else:
        print('ERROR: unknow:', s)

# --- main ---

root = tk.Tk()
root.geometry("100x100")

v = tk.StringVar(value="f")

a = tk.OptionMenu(root, v, "btn", "btn2", command=f)
a.grid(column=1, row=1)

l = tk.Label(root)
l.grid(column=1, row=2)

root.mainloop()

If you have to delete Label (because you have to put different widget - ie. Button) then use destroy()

import tkinter as tk

# --- functions ---

def f(s):
    global w # inform function to use external variable when you will use `=`

    if s == "label":
        if w: # check if widget already exist
            w.destroy()

        w = tk.Label(root, text="Hello World!")
        w.grid(column=1, row=2)

    elif s == "button":
        if w: # check if widget already exist
            w.destroy()

        w = tk.Button(root, text="Click Me")
        w.grid(column=1, row=2)

    else:
        print('ERROR: unknow:', s)

# --- main ---

root = tk.Tk()
root.geometry("100x100")

v = tk.StringVar(value="f")

a = tk.OptionMenu(root, v, "label", "button", command=f)
a.grid(column=1, row=1)

w = None # create global variable without value
         # to use later with widget (and keep access to this widget)

root.mainloop()

BTW: you can create widgets only once and replace them - then use grid_forget() to hide widget

import tkinter as tk

# --- functions ---

def f(s):
    global w # inform function to use external variable when you will use `=`

    if s == "label":
        if w: # check if widget already exist
            w.grid_forget()

        w = l
        w.grid(column=1, row=2)

    elif s == "button":
        if w: # check if widget already exist
            w.grid_forget()

        w = b
        w.grid(column=1, row=2)

    else:
        print('ERROR: unknow:', s)

# --- main ---

root = tk.Tk()
root.geometry("100x100")

v = tk.StringVar(value="f")

a = tk.OptionMenu(root, v, "label", "button", command=f)
a.grid(column=1, row=1)

# create but not display
l = tk.Label(root, text="Hello World!")
b = tk.Button(root, text="Click Me")

w = None # create global variable without value
         # to use later with widget (and keep access to this widget)

root.mainloop()

Function can be shorter if you check all possible values for s

def f(s):
    global w # inform function to use external variable when you will use `=`
    if w: # check if widget already exist
        w.grid_forget()

    if s == "label":
        w = l
    elif s == "button":
        w = b

    w.grid(column=1, row=2)
0
Bheri Giridhar Kartikeya On

Try modifying one=Label(root,text="one blah blah",width=20) and two=Label(root,text="two",width=20) or put a suitable width. It will be okay.

The code looks like this:

from tkinter import *

root=Tk()

def f(s):

  if s=="btn":

    one=Label(root,text="one blah blah",width=20)

    one.grid(column=1,row=2)

  if s=="btn2":

    two=Label(root,text="two",width=20)

    two.grid(column=1,row=2)


v=StringVar(root)

v.set("f")

a=OptionMenu(root,v,"btn","btn2",command=f)

a.grid(column=1,row=1)

root.configure()

root.geometry("100x100")

root.mainloop()

One blah blah will be erased completly and two will be written.