我做了一個 tkinter 視窗,用戶必須在其中選擇顯示兩個單選按鈕的串列框中的一些專案。如果用戶選擇一個單選按鈕,然后取消選擇串列框中的專案,則單選按鈕將被洗掉。
問題是,如果用戶選擇與以前相同的專案,則單選按鈕已被選中。我希望它們在再次創建時為空。
提前致謝
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()
uj5u.com熱心網友回復:
這是因為取消選擇的專案沒有從 中洗掉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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/327495.html
