I am using the following code to create a set of radiobuttons. The radiobuttons are created fine, but when it comes time to delete them, the buttons and text persist on screen. How do I go about ensuring that the radiobuttons get deleted?

import Tkinter

class App():
    def __init__(self):
        self.parent = Tkinter.Tk()
        self.initialize() 
        self.parent.mainloop()
 
    def initialize(self):
        self.itemVar = "abc"
        
        EVENS = [
            ('item0', 0),
            ('item2', 2),
            ('item4', 4),
            ('item6', 6),
            ('item8', 8),
            ('item10', 10),
            ('item12', 12),
            ('item14', 14),
        ]
 
        r = 0
        for text, mode in EVENS:
            self.itemEvens = Tkinter.Radiobutton(self.parent, text=text,
                        variable=self.itemVar, value=mode)
            self.itemEvens.grid(row =r, column=0)
            r += 1

        self.nextButton = Tkinter.Button(self.parent, text='Next',
                                                 command=self.nextScreen
        self.nextButton.grid(row=9, column=0)
    
    def nextScreen(self):
        self.itemEvens.destroy()

        self.questionLabel = Tkinter.Label(self.parent,
                                          text='I need the radios gone')
        self.questionlabel.grid(row=0, column=0)

if __name__ == "__main__":
    app = App()

Recommended Answers

All 3 Replies

self.itemEvens will contain the object for the last button created in the for() loop. Instead, store each individual button object in a dictionary or list. You can then delete whichever button object you choose.

store each individual button object in a dictionary or list.

Okay, I have no clue how to do this part. I have been trying to make a list of names for the objects and assign them like this:

import Tkinter

class App():
    def __init__(self):
        self.parent = Tkinter.Tk()
        self.initialize() 
        self.parent.mainloop()
 
    def initialize(self):
        self.itemVar = "abc"
        radioName = ['self.radio1','self.radio2','self.radio3','self.radio4']
        
        EVENS = [
            ('item0', 0),
            ('item2', 2),
            ('item4', 4),
            ('item6', 6),
        ]
 
        r = 0

        for text, mode in EVENS:
            radioName[r] = Tkinter.Radiobutton(self.parent, text=text,
                        variable=self.itemVar, value=mode)
            radioName[r].grid(row =r, column=0)
            r += 1

        self.nextButton = Tkinter.Button(self.parent, text='Next',
                                                 command=self.nextScreen
        self.nextButton.grid(row=9, column=0)
    
    def nextScreen(self):
        self.radio1.destroy()

        self.questionLabel = Tkinter.Label(self.parent,
                                          text='I need the radios gone')
        self.questionlabel.grid(row=0, column=0)

if __name__ == "__main__":
    app = App()

I know this is completely wrong, but I don't know how to get it right.
And, again, thank you for helping woooee.

You must pack or grid from the stored variable. Otherwise you get value None. Similarly as append and sort methods return None. That means they are procedural not functinal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.