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