在使用 Tkinter 选择和删除后创建单选按钮时如何取消选择?

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

我做了一个 tkinter window,其中用户必须 select 显示两个单选按钮的列表框中的一些项目。如果用户 select 有一个单选按钮,然后 deselect 列表框中的项目,单选按钮将被删除。
问题是如果用户 select 有相同的项目和以前一样,单选按钮已经 selected。我希望再次创建时它们是空的。
提前致谢

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()

   

这是因为未从self.dictRadioButtonValue中删除取消选择的项目。

创建 self.dictRadioButtonValue 的副本,然后从副本中删除使用的项目。最后,从 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]