Python Forum
[Tkinter] listbox constantly being added to the list.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] listbox constantly being added to the list.
#1
Hi,

   

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.

   

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.

   

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.
Reply
#2
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')
Reply
#3
I want the sinifYonetimi function to run only once, no matter how many times I press the button.
Reply
#4
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
speedev likes this post
Reply
#5
The method is really clever and really beautiful. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PysimpleGUI - listbox with list jamesaarr 2 4,231 Sep-06-2021, 09:52 AM
Last Post: jamesaarr
  [Tkinter] list box select event triggers when selecting another listbox Roshan 1 4,263 May-09-2021, 08:27 AM
Last Post: Yoriz
  How to format a list when displaying in a tkinter Listbox nortski 3 9,566 Apr-03-2018, 02:31 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020