如何使用 tkinter 不在列表中显示重复项

How to not display duplicates in a list using tkinter

如何让这个程序在已经被选中时不再向用户显示相同的选择? 我知道在幕后我可以为代码创建一组条目以继续使用,但如果用户再次选择相同的东西,我似乎无法停止显示重复的项目。我认为 'not in' 语句可能有效。有什么帮助吗?

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.")

希望对您有所帮助!