Search code examples
pythonpython-3.xtkintercustomtkinter

Python, customTkinter not assigning Label to variable


I'm trying to check wether an entry is left empty, the user entered the correct id in it or if the user entered an incorrect id. The idea I have is to basically destroy the label for each entry status and creating a new one so that the text don't overlap. When I wanted to implement that, it appears that the variable always remains empty even if the condition that creates the label is executed first.

Here is my function:

def updateWrongIdText(id):
    wrongIdText = None
    if doesIdExist(id):
        if wrongIdText is None:
            print("Still no widget in variable")
    else:
        if len(id) == 0:
            wrongIdText = customtk.CTkLabel(master=loginFrame, text="Sorry, Studnet Id case cannot be empty", font=('Chalkboard', 12), text_color="red")
            wrongIdText.place(relx = 0.5, rely = 0.8, anchor = "center")
        else:          
            wrongIdText = customtk.CTkLabel(master=loginFrame, text="Sorry, this Student ID is not registerd in our Library", font=('Chalkboard', 12), text_color="red")
            wrongIdText.place(relx = 0.5, rely = 0.8, anchor = "center")

And this is how the id is retrieved; after the click of a button and then sent to both doesIdExist() and updateWrongIdText().

def onLoginButtonClick():
    id = idEntry.get()
    password = passEntry.get()
    doesIdExist(id)
    updateWrongIdText(id)

I'm also new to StachOverflow so idk how to produce a minimal reproducible example.

Below as an MRE:

import tkinter as tk
import customtkinter as customtk

label = None

app = customtk.CTk()
app.geometry("800x600")
app.title("Library Application")



def Button1Click():
    if button1.winfo_exists() == 1:
        label.destroy()

def Button2Click():
    global label
    label = customtk.CTkLabel(app, text="This is a test.")
    label.place(relx= 0.5, rely= 0.5, anchor="center")

button1 = customtk.CTkButton(master = app, text="Click me to delete", command=Button1Click)
button1.place(relx= 0.5, rely= 0.6, anchor="center")

button2 = customtk.CTkButton(master = app, text="Click me to create", command=Button2Click)
button2.place(relx= 0.5, rely= 0.4, anchor="center")


app.mainloop()

And Thank You in advance :)

I've tried every answer I found on Google, nothing worked.


Solution

  • The solution was posted by jasonharper as a comment:

    Your if wrongIdText is None: check is pointless - of course it's None, you assigned that value two lines earlier! If you want the value to persist between calls to the function, it cannot be a local variable - make it global, or an attribute of some global object.

    Thanks to Him and to Bryan for the help!

    I fixed the problem by declaring my label's variable as global in the top of my code and assigning a None value to it using global wrongIdText and I've modified the code accordingly:

    def updateWrongIdText(id):
        global wrongIdText
        if doesIdExist(id):
            if wrongIdText is not None:
                wrongIdText.destroy()
        else:
            if len(id) == 0:
                if wrongIdText is not None:
                    wrongIdText.destroy()
                wrongIdText = customtk.CTkLabel(master=loginFrame, text="Sorry, Studnet Id case cannot be empty", font=('Chalkboard', 12), text_color="red")
                wrongIdText.place(relx = 0.5, rely = 0.8, anchor = "center")
            else:
                if wrongIdText is not None:
                    wrongIdText.destroy()          
                wrongIdText = customtk.CTkLabel(master=loginFrame, text="Sorry, this Student ID is not registerd in our Library", font=('Chalkboard', 12), text_color="red")
                wrongIdText.place(relx = 0.5, rely = 0.8, anchor = "center")