Geeting UnboundLocalError local variable pwd_lable referenced before assignment please guide how to solve this problem so that my program work

by: Tathe, 7 years ago

Last edited: 7 years ago

I have try to write program for library Management system i am using tkinter module for it. I have write the below code but when i am trying to create multiple Text box i am getting  below error please guide
=========================================================
  File "Hope_work.py", line 22, in __init__
    frame = F(container, self)
  File "Hope_work.py", line 62, in __init__
    pwd_lable.pack()
UnboundLocalError: local variable 'pwd_lable' referenced before assignment

Below is the complete program i am getting error in Pageone class
==========================================================
import tkinter as tk
import os

LARGE_FONT= ("Verdana", 12)

class Myprogramapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

        
class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Library Managment System", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = tk.Button(self, text="Admin Login",
                                            command=lambda: controller.show_frame(PageOne))

        button.pack()

        button1 = tk.Button(self, text="Lib Login")

        button1.pack()

class PageOne(tk.Frame):


    def __init__(self, parent, controller):


        tk.Frame.__init__(self, parent)
        name_label = tk.Label(self, text="User ID : ")
        pwd_label = tk.Label(self, text="Password: ")
        name_label.pack()
        pwd_lable.pack()
        name_lable = tk.Entry(self)
        pwd_lable = tk.Entry(self, show="*")
        name_lable.pack()
        pwd_lable.pack()

        button1 = tk.Button(self, text="Login")

        button1.pack()

app = Myprogramapp()
app.mainloop()



You must be logged in to post. Please login or register an account.



You're typoing.

First you defined it as: pwd_label, then you call it pwd_lable. Fix that :P

-Harrison 7 years ago

You must be logged in to post. Please login or register an account.


oops !!! Thanks for the correction....

-Tathe 7 years ago

You must be logged in to post. Please login or register an account.

The Unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context. Python doesn't have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local .   To solve this problem, you can explicitly say it's a global by putting global declaration in your function. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes a variable to global variable everywhere in the function.

http://net-informations.com/python/err/local.htm

-berkninan 4 years ago

You must be logged in to post. Please login or register an account.