0

I've made a simple registration form using tkinter and mongoengine.But due to some reason, the value grabbed from tkinter entry boxes are stored as null or empty in mongodb.I guess the problem is that the variable is not able to grab the value entered in the tkinter entry box, but I am not able to find a solution to this problem as the code looks syntactically correct.

from tkinter import *
import tkinter.messagebox
from mongoengine import *

connect('demo')

class Registration(Document):
    username = StringField(required=True,unique=True)
    password = StringField(required=True)

def register():
if len(Registration.objects(username=name)) == 1:
    tkinter.messagebox.showinfo("Oops!", "Username already taken")
else:
    entry = Registration(
        username=name,
        password=pwd
    )
    entry.save()
    tkinter.messagebox.showinfo("Notice", "Registered Successfully !")

# GUI
root = Tk()
root.title("Registration Demo")

frame = Frame(root,width=500,height=500)
frame.pack()
label1 = Label(frame,text="username")
label2 = Label(frame,text="password")

entry1 = Entry(frame)
entry2 = Entry(frame)
name = entry1.get()
pwd = entry2.get()

button = Button(frame,text="Register",command=register)

label1.grid(row=0,column=0,sticky=E)
label2.grid(row=1,column=0,sticky=E)
entry1.grid(row=0,column=1)
entry2.grid(row=1,column=1)
button.grid(columnspan=2)

root.mainloop()

And here's the mongodb table view : Mongodb Output

As you can see in the output, the id is created but the username and password fields are blank, although the user had typed an input.

Endemic
  • 370
  • 1
  • 2
  • 15
  • 1
    you are calling `entry1.get()` about a millisecond after you create the entry widget. The user won't have had a chance to type anything. – Bryan Oakley May 29 '18 at 15:02
  • entry1.get() must be called at the time you call your function `register()`. So instead of using the variable `name` you need to use `entry1.get()` there instead. – Mike - SMT May 29 '18 at 15:06
  • Thanks for explaining my mistake.That solved my problem. – Endemic May 29 '18 at 15:21

1 Answers1

1

These 2 lines:

name = entry1.get()
pwd = entry2.get()

Are not doing what you think they are doing and you can just delete them as they are not helping any.

What is happening is the value of entry1 and entry2 is being gathered at the start of your program and this will always be equal to an empty sting becuase it is called 1 time immediately when the program starts. This is only happening once and is not updated when you reference the variable name or pwd. You need to recall entry1.get() and entry2.get() from inside your function.

Change this:

def register():
    if len(Registration.objects(username=name)) == 1:
        tkinter.messagebox.showinfo("Oops!", "Username already taken")
    else:
        entry = Registration(
            username=name,
            password=pwd
        )
        entry.save()
        tkinter.messagebox.showinfo("Notice", "Registered Successfully !")

To this:

def register():
    if len(Registration.objects(username=entry1.get())) == 1:
        tkinter.messagebox.showinfo("Oops!", "Username already taken")
    else:
        entry = Registration(
            username=entry1.get(),
            password=entry2.get()
        )
        entry.save()
        tkinter.messagebox.showinfo("Notice", "Registered Successfully !")
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • 1
    Thanks for the explanation & quick response.Really appreciate it. – Endemic May 29 '18 at 15:21
  • @Endemic glad to help. Please note this applies to anything being assigned to a variable. So if you in the future have issues with something remember that calling the variable name will not re-run the code that assigned a value to that name. You will need to re-run the code at the time you need to update your data. – Mike - SMT May 29 '18 at 15:24
  • Yep, now I understand the logic. – Endemic May 29 '18 at 15:36