Python全局变量不起作用(3个模块)

时间:2015-04-08 01:36:18

标签: python mysql variables tkinter global

我正在尝试创建一个程序,在用户登录后在tkinter标签中显示用户的用户名。我正在尝试按照this webpage创建全局变量的说明进行操作。这是我到目前为止的代码的简化设置:

config.py(我保留全局变量):

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)

*这是main.py中调用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)

我的问题是:main.py中的profileName标签不会显示用户的用户名。它只显示默认值,即“名称” 奇怪的是,在我登录后,mod.py中的“print config.nameInput”会打印用户名。但由于某种原因,它只是没有显示在标签上。 如果您需要更多信息,请告诉我。我真的需要解决这个问题才能继续我的项目。

0 个答案:

没有答案