Python Forum
[Tkinter] listbox constantly being added to the list. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] listbox constantly being added to the list. (/thread-33903.html)



listbox constantly being added to the list. - speedev - Jun-08-2021

Hi,

[attachment=1122]

I made a simple left-justified menu with buttons. Assign functions as commands to buttons. That is, the menu contents are within the functions.

Now there is a problem.

I am pulling the data in this list via MongoDB. The items in this list are the data in the MongoDB collection.

[attachment=1123]

When I click on the "Sınıf Yönetimi" button from the left-justified menu with the buttons, the sinifYonetimi function works.

But as I run the SinifYonetimi function through the menu, it constantly re-inserts the items in MongoDB.

[attachment=1124]

As you can see in the picture above, the data has been inserted 3 times. This is because I pressed the "Sınıf Yönetimi" button from the left menu 3 times. Upon pressing it, the sinifYonetimi function ran 3 times and inserted 3 times.


It's a bug and I haven't been able to fix it. How can I solve this problem?

Code:

def sinifYonetimi():
    sinifyonetimiaciklama = Label(fg="white", bg="#202020")

    veriyicek = siniflar.find().sort("sinif")
    i = 1

    for x in veriyicek:
        print(x["sinif"])

        liste.insert(i, x["sinif"])
    i = i + 1

    liste.configure(width=50)
    sinifeklebuton.grid(column=3, row=5, sticky=E)# Buton
    liste.grid(column=2, columnspan=3)
    sinifadiyazma.grid(column=3, row=5, sticky=NW)# Entry
    sinifadiyazma.configure(width=30)

    silbuton.grid(column=2, columnspan=3)
    silbuton.configure(width=10, height=1)
    islemsec.grid_remove()

menubuton2 = Button(text="Sınıf Yönetimi", fg="white", bg="#282528", height=4, command=sinifYonetimi)
How can I solve the problem? I don't want it to be re-inserted all the time. It is constantly re-inserting as I press the button from the menu. How can I prevent it from re-inserting? Thanks in advance.


RE: Tkinter listbox sürekli olarak listeye ekleniyor. - Yoriz - Jun-08-2021

I don't really understand what you want to happen, if you press it 3 times the event handler will be called 3 times.
Do you want to only be able to press it once and then are no longer able to, if so you can disable the button with

button.config(state='disabled')



RE: Tkinter listbox sürekli olarak listeye ekleniyor. - speedev - Jun-08-2021

I want the sinifYonetimi function to run only once, no matter how many times I press the button.


RE: Tkinter listbox sürekli olarak listeye ekleniyor. - Yoriz - Jun-08-2021

add this to the start of the event handler function and the button won't be able to be pressed anymore.
def sinifYonetimi():
    menubuton2.config(state='disabled')
Please see Namespace flooding with * imports


RE: Tkinter listbox sürekli olarak listeye ekleniyor. - speedev - Jun-08-2021

The method is really clever and really beautiful. Thank you.