pythontkintertypeerrortoplevel

TypeError: 'Toplevel' object is not callable


I made a function in python

import tkinter as tk
import re
from tkinter import filedialog
from tkinter import messagebox
import theFile.fileCentral as fc
import theData.workers as workerData
import theData.contracts as contractData

def openWorkers():
    workerData.workerWindow(root, tk, scrHeight, scrWidth)

The workerWindow function opens another window using the tkinter module, which I can close. But when I try to open it again, I get the error described in the title

contractData.contractWindow(root, tk, scrHeight, scrWidth)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'Toplevel' object is not callable

I asked Google, and as I understand, it is about how I handle and use the variables. Link

The function contractWindow is in its own file like this


openContractFlag = False



def contractWindow(contractRoot, contractTk, contractHeight, contractWidth):
    global openContractFlag                         
    contractWidth = int((contractWidth/2) - 600)                 
    contractHeight = int((contractHeight/2) - 500)               
    if openContractFlag == False:                   
        openContractFlag = True                     
        openContractWindow(contractRoot, contractTk, contractWidth, contractHeight) 



def openContractWindow(contractRoot, contractTk, contractWidth, contractHeight):
    global contractWindow        
    contractWindow = contractTk.Toplevel(contractRoot)                                                                                                                    
    contractWindow.title("Overenskomster")                                                                                                                
    contractWindow.geometry(f"900x800+{contractWidth}+{contractHeight}")                                                                                                  

    # Create new buttons in the new window                                                                                                                
    contractCloseButton = contractTk.Button(contractWindow, text="Luk", command=contractClose, bg="blue", fg="white", font=("Helvetica", 16, "bold"))                     
    contractCloseButton.place(x=100, y= 10)                                                                                                                       

    newContract = contractTk.Button(contractWindow, text="Opret ny overenskomst", command=makeNewContract, bg="blue", fg="white", font=("Helvetica", 16, "bold")) 
    newContract.place(x=200, y= 10)                                                                                                                       

    label = contractTk.Label(contractWindow, text="En liste med alle overenskomster", borderwidth=2, bg="black", fg="white", font=("Helvetica", 16, "bold"))      
    label.place(x=270, y=70)                                                                                                                              


def contractClose():
    global openContractFlag
    if contractWindow is not None:
        openContractFlag = False
        contractWindow.destroy()

def makeNewContract():
    print("Make new contract")

I renamed the variables in this file/code block, to make sure they are not used elsewhere, but no success.


Solution

  • check this code.

    openContractFlag = False
    
    def contractWindow(contractRoot, contractTk, contractHeight, contractWidth):
        global openContractFlag
        contractWidth = int((contractWidth/2) - 600)
        contractHeight = int((contractHeight/2) - 500)
        if openContractFlag == False:
            openContractFlag = True
            openContractWindow(contractRoot, contractTk, contractWidth, contractHeight)
    
    def openContractWindow(contractRoot, contractTk, contractWidth, contractHeight):
        global openContractFlag  # Rename this variable if needed
        global contractWindowFlag  # Rename the global variable
        contractWindowFlag = contractTk.Toplevel(contractRoot)
        contractWindowFlag.title("Overenskomster")
        contractWindowFlag.geometry(f"900x800+{contractWidth}+{contractHeight}")
    
        # ... rest of the code ...
    
    def contractClose():
        global openContractFlag
        global contractWindowFlag  # Update the variable name here
        if contractWindowFlag is not None:
            openContractFlag = False
            contractWindowFlag.destroy()
    
    def makeNewContract():
        print("Make new contract")