当该程序已被选中时,如何使其不再向用户显示相同的选择?我知道在幕后我可以创建一组条目供代码继续使用,但如果用户再次选择相同的项目,我似乎无法停止显示重复的项目。我认为"不在"的说法可能奏效了。请帮忙吗?
from tkinter import *
from tkinter import ttk
root = Tk()
# set in pixels
root.geometry("1000x750+100+100")
my_list = set()
def combo_click(event):
my_label = Label(root, text=myCombo.get()).pack()
if myCombo.get() not in my_list:
my_list.add(myCombo.get())
# print('List without duplicate items (Set) ' + 'n')
OptionList = [
"Aries",
"Taurus",
"Gemini",
"Cancer"
]
clicked = StringVar()
clicked.set(OptionList[0])
# *Options - https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists
#drop = OptionMenu(root, clicked, *OptionList, command=selected)
#drop.pack(pady=100)
myCombo = ttk.Combobox(root, value=OptionList)
myCombo.current(0)
myCombo.bind("<<ComboboxSelected>>", combo_click)
myCombo.pack()
root.mainloop()
您可以使用以下代码来过滤列表中的重复项。我假设您通过变量将每个文本添加到Tkinter列表中。
在我的代码中,变量:a是我们希望添加到列表中的项
筛选Tkinter列表条目的代码:
While True: #replace the bit inbetween w and t with your how long you want to do it
#add everything you add to a list before you add to Tkinter Listbox as shown below.
for x in tlist:
if tlist contains a:
print("Duplicate! This will not be added.")
else:
[whatever_you_called_your_listbox].insert(END,a)
print(a,"Was added.")
希望这能有所帮助!