Home > Software design >  How to deselect a radiobutton when it is created after to be selected and deleted with Tkinter?
How to deselect a radiobutton when it is created after to be selected and deleted with Tkinter?

Time:10-20

I did a tkinter window where an user has to select some items in a listbox which displays two radiobuttons. If the user selects one radiobutton and then deselects the item in the listbox, radiobuttons are deleted.
The problem is that if user selects the same item as previously, the radiobutton is already selected. I would like they are empty when they are created again.
Thanks in advance

from tkinter import *
import tkinter as tk

class Application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.dictLabel = dict()
        self.createWidgets()

    def createWidgets(self):
        self.ListNumber = ['one', 'two', 'three', 'four']
        self.labelListNumber = tk.Label(self, text=' Select a Number : ')
        self.labelListNumber.place(x=40, y=30)

        self.frame = Frame(self)
        self.frame.place(x=200, y=30)
        self.list = Listbox(self.frame, exportselection=False,activestyle = tk.NONE, height=5, selectmode="multiple")
        self.list.pack(side='left', fill='y')

        for each_item in range(len(self.ListNumber)):
            self.list.insert(END, self.ListNumber[each_item])
        self.scrollbar = Scrollbar(self.frame, orient="vertical", command=self.list.yview)
        self.scrollbar.pack(side='right', fill='y')
        self.list.config(yscrollcommand=self.scrollbar.set)

        self.dictRadioButtonValue = dict()
        self.list.bind('<<ListboxSelect>>',self.createRadioButton)

    def createRadioButton(self, evt):
        index = self.list.curselection() # grab the index
        c = 1
        if len(index) == 0 or len(self.dictLabel) != 0:
            for e in self.dictLabel:
                self.dictLabel[e][0].place_forget()
                self.dictLabel[e][1].place_forget()
                self.dictLabel[e][2].place_forget()

            del self.dictLabel
            self.dictLabel = dict()
        for i in index:
            item = self.list.get(i)
            if not item in self.dictRadioButtonValue:
                if len(self.dictRadioButtonValue) > len(index):
                    if not item in self.dictLabel[item]:
                        del self.dictRadioButtonValue[item]
                else :
                    radioButtonValue = tk.IntVar()
                    radioButtonValue.set(' ')
                    self.dictRadioButtonValue[item] = radioButtonValue

            L = tk.Label(self, text=f"Number selected is {item}")

            radiobtn5 = tk.Radiobutton(self, text="Yes", variable = self.dictRadioButtonValue[item], value = 5)
            radiobtn7 = tk.Radiobutton(self, text="No", variable = self.dictRadioButtonValue[item], value = 6)

            L.place(x=350, y=10 (c * 20))
            radiobtn5.place(x=500, y=10   (c * 20))
            radiobtn7.place(x=550, y=10   (c * 20))
            self.dictLabel[item] = L, radiobtn5, radiobtn7
            c = c 1

if __name__ == "__main__":
    app = Application()
    app.geometry("700x250")
    app.mainloop()

   

CodePudding user response:

It is because the deselected item is not removed from self.dictRadioButtonValue.

Create a copy of self.dictRadioButtonValue and then remove the items that are used from the copy. At the end, remove remaining items in copy from self.dictRadioButtonValue:

    def createRadioButton(self, evt):
        index = self.list.curselection() # grab the index
        c = 1
        if len(index) == 0 or len(self.dictLabel) != 0:
            for e in self.dictLabel:
                self.dictLabel[e][0].place_forget()
                self.dictLabel[e][1].place_forget()
                self.dictLabel[e][2].place_forget()

            del self.dictLabel
            self.dictLabel = dict()

        copy = self.dictRadioButtonValue.copy()
        for i in index:
            item = self.list.get(i)
            if not item in self.dictRadioButtonValue:
                # new item selected
                radioButtonValue = tk.IntVar(value=' ')
                self.dictRadioButtonValue[item] = radioButtonValue
            else:
                # remove current item from copy
                del copy[item]

            L = tk.Label(self, text=f"Number selected is {item}")

            radiobtn5 = tk.Radiobutton(self, text="Yes", variable = self.dictRadioButtonValue[item], value = 5)
            radiobtn7 = tk.Radiobutton(self, text="No", variable = self.dictRadioButtonValue[item], value = 6)

            L.place(x=350, y=10 (c * 20))
            radiobtn5.place(x=500, y=10   (c * 20))
            radiobtn7.place(x=550, y=10   (c * 20))
            self.dictLabel[item] = L, radiobtn5, radiobtn7
            c = c 1

        # remove remaining items in copy from self.dictRadioButtonValue
        for item in copy:
            del self.dictRadioButtonValue[item]
  • Related