-1

I have a tkinter window with a button that opens a Toplevel window. I have restricted the no. of windows with the help of counters.

But every time I click the button to open the window the WM_DELETE_WINDOW protocol executes (which is tied to the reset counter function) and resets my counter.

from tkinter import *

root = Tk()

# root stuff

btn1 = Button(root,text="Add Book",command=lambda: [add_open(), addBook()]).pack()
# add_open() is the counter function

root.mainloop()

addBook() function defined in a saparate file

def addBook():

    if add_check(): # check counter
        AddBookWindow = Toplevel()
        
        # more block of code

        AddBookWindow.protocol("WM_DELETE_WINDOW", add_close()) # reset counter
        AddBookWindow.mainloop()

Counter functions defined in a different file

def add_check():
    with open(r"counters\addbook.txt", "r") as file:
        a = file.read()
    return True if a == "1" else False


def add_open():
    with open(r"counters\addbook.txt", "a") as file:
        file.write("1")


def add_close():
    print("add close")
    with open(r"counters\addbook.txt", "w") as file:
        file.write("")
  • You actually executed `add_close()` and passed the result to `protocol()`. Use `AddBookWindow.protocol("WM_DELETE_WINDOW", add_close)` instead. – acw1668 Nov 25 '21 at 06:02

1 Answers1

0

You executed add_close() and passed the result to protocol(). Use AddBookWindow.protocol("WM_DELETE_WINDOW", add_close) instead. (Similar to acw1668's answer.)

Rydex
  • 395
  • 1
  • 3
  • 9