I'm creating an interface that first prompts the user to login to the database. I'm having trouble with the get method for an Entry object. For the 'Login' button command, I use lambda since I am calling the login function which takes arguments. Since I'm passing Entry objects into my login function I call user.get() and pw.get() in those functions.
Upon running this code however, it says that user.get(), pw.get() are Nonetype objects with no attribute get. I don't understand why the entries are Nonetype since the logintoDB should be called after the buttons are created.
Thanks for the help.
Below is my code:
import Tkinter as tk
import tkFileDialog as tkfd
import tkMessageBox as tkmb
import xlrd
def openFile():
#returns an opened file
fname = tkfd.Open(filetypes = [("xls files","*.xls")])
fpath = fname.show()
if fname:
try:
TLA_sheet = xlrd.open_workbook(fpath).\
sheet_by_name('TLA - TOP SKUs')
tk.Button(root, text = "Import TLAs", command = lambda: importTLAtoDB(TLA_sheet)).pack()
tkmb.showinfo("Success!", "Spreadsheet successfully loaded. \n\
Click Import TLAs to load TLA info into RCKHYVEDB database.")
except:
tkmb.showerror("Error", "Failed to read file\n '%s'\n\
Make sure file is a type .xls" % fpath)
def enter(event):
return logintoDB
def logintoDB(user, pw):
#request login for database access
print user.get(), pw.get()
try:
db = MySQLdb(config.server_link, user.get(), pw.get(), config.database)
tkmb.showinfo("Success!","Database login successful.\n\
Click Browse to load TLA spreadsheet.")
tk.Button(root, text = "Browse", command = openFile, width = 10).pack()
return True
except:
tkmb.showerror("Error", "Login failed. Try again.")
return False
#GUI setup
root = tk.Tk()
root.title("TLA Database Tool")
user_label = tk.Label(root, text = "username").pack()
user = tk.Entry(root, textvariable = tk.StringVar()).pack()
pw_label = tk.Label(root, text = "password").pack()
pw = tk.Entry(root, show = "*", textvariable = tk.StringVar()).pack()
login_button = tk.Button(root, text = "Login", command = lambda: logintoDB(user,pw)).pack()
root.mainloop()