-3

I have try to write program for library Management system I am using tkinter module for it. I have writen the below code but when I am trying to create multiple Text box i am getting below error.

  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.name_lable, text="Password:")
        name_label.pack(pady=10,padx=10)
        pwd_lable.pack(pady=10,padx=10)
        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()
if __name__ == "__main__":
    app = Myprogramapp()
    app.mainloop()

**

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Tathe
  • 3
  • 2
  • You have several problems here. Indention for one, spelling for label names is miss matched due to spelling errors, you are creating the `pwd_label` assigned to another widget instead of a frame or the root window. Your classes are set up in a strange way and I do not think they will work like this. Try placing everything into one class it would make more sense here. For any of the widgets you will be working with/updating you need to set them as class attributes using the `self.` prefix. – Mike - SMT Jul 19 '17 at 21:12
  • The code you posted does not give the error you say it does. – Bryan Oakley Jul 19 '17 at 21:13
  • @SierraMountainTech: the code design came either directly or indirectly from http://stackoverflow.com/questions/7546050. Unfortunately someone copied it without understanding it, created a video tutorial using it but without properly explaining it, and now lots of beginners use this as a starting template. _sigh_ – Bryan Oakley Jul 19 '17 at 21:16
  • @BryanOakley: So I am guessing this is not the first time you have seen this bit of code floating around on stack overflow then. – Mike - SMT Jul 19 '17 at 21:19
  • @SierraMountainTech: I see this question a _lot_. There are well over 100 questions that contain the class `PageOne`, and many more where they changed the class name. There are 300 or so with `show_frame`, most of which are variations of this code. – Bryan Oakley Jul 19 '17 at 21:22

1 Answers1

0

It would appear that you are trying to bite off more than you can chew so to speak with this example of code. You are miss using parts of tkinter that should be understood before moving onto something more complicated like this.

Before you try to use multiple classes like this try to focus more on understanding how tkinter works and how to implement all its widgets properly in a single class or even outside of a class first.

You do not assign widgets to another widget like you are attempting to do here:

pwd_label = tk.Label(self.name_lable, text="Password:")

This is the problem refereed to in your trackback. You need to assign the Label widget to either a root window, a frame or a toplevel.

Your indention is not clean and if the way you have it pasted in your question is accurate then you code will not work as the def show_frame() method is not inside the Myprogramapp class.

You are importing os for no reason here and it is not good practice to import libraries you are not currently using.

You should make some important parts of your program into a class attribute, like your entry fields. If you are going to put in a password to that field and try and get() the string from the entry field inside a method you wont be able to as its not a class attribute. You can fix this by adding self. prefix to the widget name.

All and all after making those changes you will get a tkinter window with 2 buttons. The Admin Login button will display a login screen. That being said I think you should either step away from classes all together while learning tkinter or work in a single class for now until you have a solid understanding of how classes, methods, and attributes work and are used.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79