Reputation: 7448
I am trying to use a entry widget for password input, a handler function for password verification and a label widget for displaying the result of the verification. The code is as following so far,
class LoginFrame(Frame):
def __init__(self, parent):
#Frame.__init__(self, parent)
super(LoginFrame, self).__init__()
self.parent = parent
self.initUI()
# initialize the login screen UI
def initUI(self):
# Set up login frame properties
self.parent.title("Login Screen")
# creating instruction label
inst_lbl = self.make_label(self.parent, "Please login to continue")
# creating labels and entries for user name and password
user_name = self.make_entry(self.parent, caption="User Name:")
pwd = self.make_entry(self.parent, caption="User Password:", show="*")
# create a login button
login_btn = self.make_button(self.parent, self.verify_user, "Login")
# create a button
#----------------------------------------------------------------------
def make_button(self, parent, command, caption=NONE, side=TOP, width=0, **options):
"""make a button"""
btn = Button(parent, text=caption, command=command)
if side is not TOP:
btn.pack(side=side)
else:
btn.pack()
return btn
def make_label(self, parent, caption=NONE, side=TOP, **options):
label = Label(parent, text=caption, **options)
if side is not TOP:
label.pack(side=side)
else:
label.pack()
return label
def make_entry(self, parent, caption=NONE, side=TOP, width=0, **options):
#Label(parent, text=caption).pack(side=LEFT)
self.make_label(self.parent, caption, side)
entry = Entry(parent, **options)
if width:
entry.config(width=width)
if side is not TOP:
entry.pack(side=side)
else:
entry.pack()
return entry
# verify user name and password
#----------------------------------------------------------------------
def verify_user(event):
"""verify users"""
if user_name.get() == "admin" and pwd.get() == "123":
#inst_lbl.configure(text="User verified")
event.widget.config(text="User verified")
else:
#inst_lbl.configure(text="Access denied. Invalid username or password")
event.widget.config(text="Access denied. Invalid username or password")
def main():
top = Tk()
app = LoginFrame(top)
top.mainloop()
if __name__ == '__main__':
main()
now, I need user_name
and pwd
to be verified by the method verify_user
, the result is shown in inst_lbl
whenever the button login_btn
is clicked (to trigger the verification). So how to bind inst_lbl
to verify_user
, in response to the login_btn
click event? and how to get the contents of user_name
and pwd
to get verified in verify_user
?
Upvotes: 0
Views: 197
Reputation: 7735
Make them class variable by adding self.
prefixes. This way you can access them outside of the method they are created.
self.inst_lbl = self.make_label(...)
self.user_name = self.make_entry(...)
self.pwd = self.make_entry(...)
When trying to call them, you also will need self.
prefixes.
self.inst_lbl.configure(...)
Other than that, you'll be needing self
parameter in verify_user
method since it's a member of a class.
Also, for Python-2.x, you'll get TypeError: must be type, not classobj
on your super(LoginFrame, self).__init__()
. The line you commented out is the right one to use.
Frame.__init__(self, parent)
If you want to use super()
, you should add object
as parameter to your class definiton because of old style classes etc.
class LoginFrame(Frame, object):
Upvotes: 1