user interface - Python GUI login using Tkinter -
i'm creating interface first prompts user login database. i'm having trouble method entry object. 'login' button command, use lambda since calling login function takes arguments. since i'm passing entry objects login function call user.get() , pw.get() in functions.
upon running code however, says user.get(), pw.get() nonetype objects no attribute get. don't understand why entries nonetype since logintodb should called after buttons created.
thanks help.
below code:
import tkinter tk import tkfiledialog tkfd import tkmessagebox tkmb import xlrd def openfile(): #returns 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 loaded. \n\ click import tlas load tla info rckhyvedb database.") except: tkmb.showerror("error", "failed read file\n '%s'\n\ make sure file type .xls" % fpath) def enter(event): return logintodb def logintodb(user, pw): #request login 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 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()
the following lines wrong in code:
user = tk.entry(root, textvariable = tk.stringvar()).pack() pw = tk.entry(root, show = "*", textvariable = tk.stringvar()).pack()
clearly variables user
, pw
above point entry
widget objects, not textvariable
associated objects.
rather should set new variable using textvariable
attribute , pass parameter using lambda operator.
here's simple example fetch text text
widget.
from tkinter import * root = tk() svalue = stringvar() # defines widget state string w = entry(root,textvariable=svalue) # adds textarea widget w.pack() def act(): print "you entered" print '%s' % svalue.get() foo = button(root,text="press me", command=act) foo.pack() root.mainloop()
notice how set separate variable svalue
, passed to textvariable
attribute of entry
widget.
in reply comment
in fact closely @ widget creation, creating widget , packing in same line , keeping reference like:
usr = entry().pack()
since pack()
returns null
, left null
value usr
.
instead this:
usr = entry(root,textvariable=svalue) # create here usr.pack() # pack
now usr
have reference entry
widget.
in fact got entire program work changing lines to:
user = tk.entry(root, textvariable = tk.stringvar()) user.pack() pw_label = tk.label(root, text = "password").pack() pw = tk.entry(root, show = "*", textvariable = tk.stringvar()) pw.pack()
Comments
Post a Comment