0

The first function(widget_cancel) is used to remove the widgets from a gui using tkinter.

The second function is used to create the labels and entry widgets in the gui.

The problem that I am facing is that despite globally declaring the variable names in the second function(adding) …I seem to be getting a error while calling the first function(widget_cancel), the error is below this code and I have tried .destroy() too but it doesnt seem to work

THE CODE IS:

def widget_cancel():

    Username.place_forget()



def ADDING():  
    global Username,passw,addrss,emil,lblfrstrow,lblsecrow,lblrdrow,lblfrtrow,lblfifrow,lblsixrow,ag,imagecalen,okbtn,submitbtn

    # Definging the first row 
    lblfrstrow = tk.Label(root, text ="Username -", ) 
    lblfrstrow.place(x = 50, y = 20) 

    Username = tk.Entry(root, width = 35) 
    Username.place(x = 150, y = 20, width = 100) 
           
    lblsecrow = tk.Label(root, text ="Password -") 
    lblsecrow.place(x = 50, y = 50) 
          
    passw = tk.Entry(root, show="*", width = 35) 
    passw.place(x = 150, y = 50, width = 100) 
          
    lblrdrow = tk.Label(root, text ="address -", ) 
    lblrdrow.place(x = 50, y = 70) 

    addrss = tk.Entry(root, width = 35) 
    addrss.place(x = 150, y = 70, width = 100) 

    lblfrtrow = tk.Label(root, text ="DOB -", ) 
    lblfrtrow.place(x = 50, y = 90) 

    lblfifrow = Label(root, text ="email -", ) 
    lblfifrow.place(x = 50, y = 110) 

    emil = tk.Entry(root, width = 35) 
    emil.place(x = 150, y = 110, width = 100) 

    lblsixrow = tk.Label(root, text ="age -", ) 
    lblsixrow.place(x = 50, y = 130)      

    ag = tk.Entry(root, width = 35) 
    ag.place(x = 150, y = 130, width = 100)

        
    imagecalen= Button(root, text ="c",  
                              bg ='blue', command = image)
    imagecalen.place(x=130, y=90, width = 10)

    okbtn = Button(root, text ="ok",  
                              bg ='white', command = rom) 
    okbtn.place(x = 150, y = 230, width = 55) 


    submitbtn = Button(root, text ="Submit",  
                              bg ='blue', command = submit) 
    submitbtn.place(x = 150, y = 320, width = 55)

THE ERROR IM GETTING IS:

 Username.place_forget()
NameError: name 'Username' is not defined

How do I resolve this...thanks in advance...btw in writing the code in sublime text editor and running it on git bash

tyzion
  • 260
  • 1
  • 10
  • Are you calling the first function before the second? – kaya3 Dec 31 '20 at 14:54
  • The first does not declare the name global. – MisterMiyagi Dec 31 '20 at 14:54
  • Nope widget cancel is called before adding @kaya3 – tyzion Dec 31 '20 at 15:00
  • `global` doesn't create global variable but only inform function to use external/global variable instead of creating local variable when you run `Username = ...` in function. To create global variable you would have to assign some value outside all functions - ie. `Username = None` . But because you use `Username.place_forget()` before you assing `Username = tk.Entry(..)` then using `Username = None` you would also check `if Username is not None: Username.place_forget()` – furas Dec 31 '20 at 17:29
  • @furas sorry for the dumb question im kinda new but let me get this straight .....so for me to get this function to run i would need to declare username = None outside the functions and also write the if statement inside the 'widget_cancel' function? – tyzion Jan 01 '21 at 10:20
  • you don't show full code but it seems code executes `widget_cancel()` before `ADDING()` so it executes `Username.place_forget()` before `Username = tk.Entry(root, width = 35)` , so it tries remove element which don't exist yet. One solution is to execute `ADDING()` before `widget_cancel()` and then it will create `Username = tk.Entry(..)` before `Username.place_forget()` and all will be OK. But if for some reason you have to run `widget_cancel()` before `ADDING()` then you have to create (somehow) variable `Username = ???` before executing `widget_cancel()` - and then you can use `None` ... – furas Jan 01 '21 at 12:20
  • ... and then you can use `None` like `Username = None` outside functio and you will have global `Username`. But then when you run `widget_cancel()` and `Username.place_forget()` then you get `None.place_forget()` which will gives error. And this will need `if Username is not None: Username.place_forget()`. – furas Jan 01 '21 at 12:24

0 Answers0