0

I am trying to create a program that displays a user's username in a tkinter label after he has logged in. I am trying to follow the instructions for creating global variables found on this webpage. Here is my (simplified) setup for the code so far:

config.py (where I keep my global variables):

nameInput = "Name" # default value of nameInput
print nameInput # prints default value of nameInput just to see if it will change

mod.py:

import config
import MySQLdb
import Tkinter as tk

#LOGIN FUNCTION 
#Username list
cursor.execute("SELECT Username FROM Student")
username_list = cursor.fetchall()
usernameList = [l[0] for l in username_list]

#Password list
cursor.execute("SELECT Password FROM Student")
password_list = cursor.fetchall()
passwordList = [l[0] for l in password_list]

login_dictionary = dict(zip(usernameList, passwordList))

def loginFunction(c,ue,pwe,mf,sp):
    #nameInput = user input from usernameEntry, which is in main.py
    config.nameInput = ue.get()
    print config.nameInput #prints nameInput again to see if it has changed to user-inputted entry, which it does
    #pwInput is user input from pwEntry
    pwInput = pwe.get()

    #If the username is correct but the password is not...
    if config.nameInput in login_dictionary:
        if pwInput != login_dictionary[config.nameInput]:
        badLoginLabel = tk.Label(mf, text="Invalid Login",
                                 bg="white", fg="red")
        badLoginLabel.grid(row=3, column=1)
        badLoginLabel.after(3000, badLoginLabel.destroy)
    # But if the user name and password are correct... 
    else:
        c.show_frame(sp)
    #If the username AND password are incorrect...      
    else:
        badLoginLabel = tk.Label(mf, text="Invalid Login",
                                 bg="white", fg="red")
        badLoginLabel.grid(row=3, column=1)
        badLoginLabel.after(3000, badLoginLabel.destroy)

main.py:

import config
import mod
import Tkinter as tk

class UserProfile(tk.Frame):
    def __init__(self, parent, controller):
               tk.Frame.__init__(self,parent)

    header = Header(self, controller)
    header.grid(row=0,column=0)

    #creates frame for main content
    mainFrame = tk.Frame(self, width=windowWidth, height=500, bg="white")
    mainFrame.grid(row=3,column=0)

    #User Profile Name
    profileName = tk.Label(mainFrame, text=config.nameInput,
                          bg="white", font=("TkDefaultFont", 14),
                           anchor=tk.W)
    profileName.grid(row=0,column=0, padx=5, pady=2, sticky=tk.W)

    mainFrame.grid_propagate(False)

*Here is the "login page" in main.py that calls loginFunction:

class LoginPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        #creates frame for main content
        mainFrame = tk.Frame(self, width=windowWidth, height=500, bg="white")
        mainFrame.grid(row=3,column=0)
        instructions = tk.Label(mainFrame, text="Login",
                             bg="white", anchor=tk.W)
        instructions.grid(row=0,column=0, padx=10, pady=10, sticky=tk.W)

        #Username field
        usernameLabel = tk.Label(mainFrame, text="Username:",
                                 bg="white")
        usernameLabel.grid(row=1,column=0, padx=10, pady=5)
        usernameEntry = tk.Entry(mainFrame, width="20")
        usernameEntry.grid(row=1,column=1)

        #Password field
        pwLabel = tk.Label(mainFrame, text="Password:",
                                 bg="white")
        pwLabel.grid(row=2,column=0, padx=10, pady=5)
        pwEntry = tk.Entry(mainFrame, width="20", show="*")
        pwEntry.grid(row=2,column=1)

        #login button
        loginButton = tk.Button(mainFrame, text="Login",
                                command=lambda: mod.loginFunction(controller,usernameEntry,
                                                              pwEntry, mainFrame,
                                                              StartPage))
        loginButton.grid(row=3,column=0)

        mainFrame.grid_propagate(False)

My problem is this: the profileName label in main.py will not display the user's username. It will only display the default value, which is "Name" The strange thing is that right after I log in, "print config.nameInput" in mod.py DOES print the username's name. But for some reason it just doesn't display in the label. If you need more information let me know. I really need to solve this problem in order to proceed on my project.

user2615805
  • 3
  • 1
  • 4
  • Have you seen [this](http://stackoverflow.com/a/13034908/1189040) ? – Himal Apr 08 '15 at 02:16
  • Yes, I have seen that. Unfortunately, I am unsure how to call loginFunction in my UserProfile class so that it can recognize that nameInput has changed. I am a bit new at this, sorry. – user2615805 Apr 08 '15 at 02:33
  • You could use `textvariable` (with a StringVar) for your `usernameLabel`. so, If the variable is changed, the label text is updated. – Himal Apr 08 '15 at 02:52
  • I tried that. I am not sure if I am doing it right but basically, I replaced text=config.nameInput in the usernameLabel to textvariable = config.nameInput And before that, I typed "config.nameInput = tk.StringVar()" All it does is display PY_VAR0 instead of the default value "Name" – user2615805 Apr 08 '15 at 03:11
  • You have to use `set()` and `get()` with that.like, `config.nameInput.set('Name')` – Himal Apr 08 '15 at 03:25
  • 1
    Your advice worked, thanks!!! – user2615805 Apr 08 '15 at 04:24
  • 1
    @Himal: you should write an answer... – Eric Levieil Apr 08 '15 at 06:48
  • 1
    @EricLevieil feel free to post an answer. – Himal Apr 08 '15 at 12:42

0 Answers0