how to clear listbox of previous selected radio button from the window after selecting another radio button using tkinker

Question:

I am an absolute beginner in programming and to Python. I am trying to create a gui using tkinter.
In this program, I want click any radio button and see the list of software name.
Here is the code:


import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint as pp
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name(r"C:UsersUSEROneDriveDesktopclient_secret.json",scope)
client = gspread.authorize(creds)
sheet = client.open("soft_list").sheet1 
l1 = sheet.get_all_records() 
 
# Import the required libraries
import tkinter
from tkinter import *
from tkinter import ttk

win = Tk()
win.title("Unauthorized Software Detection")
win.geometry("500x400")

# Define a function to get the output for selected option
def selection():
   selected = "Unauthorized software List: " + "n"
   label.config(text=selected)
   col = sheet.col_values(radio.get())
   listbox = tkinter.Listbox(win)
   listbox.pack(anchor=E)
   j = 0
   for i in col:
        listbox.insert(j, i)
        j = j + 1
   #listbox.place(relx = 0.5, rely = 0.5, anchor="NE")
radio = IntVar()
Label(text="Welcome to the unauthorized software detection app", font=("Roboto", 15)).pack()
Label(text="The List of Computers:", font=('Aerial 11')).place(x = 40, y = 60)

# Define radiobutton for each options
r1 = Radiobutton(win, text="computer1", variable=radio, value=2, command=selection).place(anchor="nw", x=40, y=90)
r2 = Radiobutton(win, text="Computer2", variable=radio, value=3, command=selection).place(anchor="nw", x=40, y=110)
r3 = Radiobutton(win, text="Computer3", variable=radio, value=4, command=selection).place(anchor="nw", x=40, y=130)


# Define a label widget
label = Label(win)
label.pack(anchor=E)
win.mainloop()

when I run this code it shows:

enter image description here

But the problem is when I select another radio button, the display of listbox from the previous selection still remains.

enter image description here

How to clear the display of listbox from previous selection and only show the selected radiobutton display?

Asked By: ijahan

||

Answers:

It is because you have created new instance of Listbox whenever selection() is executed.

You need to create the listbox once and update its content inside the function:

...
# Define a function to get the output for selected option
def selection():
   selected = "Unauthorized software List:"
   label.config(text=selected)
   listbox.delete(0, "end") # clear listbox
   # populate listbox
   col = sheet.col_values(radio.get())
   for i in col:
        listbox.insert("end", i)

...
# Define a label widget
label = Label(win)
label.pack(anchor=E)

# create listbox once
listbox = tkinter.Listbox(win)
listbox.pack(anchor=E)

win.mainloop()
Answered By: acw1668
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.