Skip to content Skip to sidebar Skip to footer

After The Login Button Is Pressed, How To Obtain The Value Of Tkinter Entry And Pass It To The SQL Query?

In the below code two tkinter entry is created entry1 and entry2 respectively for username and password. What I am looking for is storing the value entered in the tkinter entry to

Solution 1:

The obvious problem is you are getting the text from both Entries directly after they have been initialized. The two variables you are using for this will not be changed when the Text in the Entries changes.

Here is some basic code how to retrieve the values from the two entry fields and pass them to a function:

import tkinter as Tk
import tkinter.messagebox


def show_pass_user(password, user):
    # show what we got
    tkinter.messagebox.showinfo("Data received", "Hey just got your username \"" + user + "\"" +
                                " and password \"" + password + "\"")
    # run your sql here


def main():
    root = Tk.Tk()

    entry_user = Tk.Entry(root)
    entry_user.insert(0, "Username")

    entry_pass = Tk.Entry(root)
    entry_pass.insert(0, "Password")

    # use a lambda to get Username and Password when button is pressed
    pressme = Tk.Button(root, text="Press Me", command=lambda:
        show_pass_user(entry_pass.get(), entry_user.get()))

    entry_user.grid()
    entry_pass.grid()
    pressme.grid()
    root.mainloop()

if __name__ == "__main__":
    main()

This code runs only with Python3!


Post a Comment for "After The Login Button Is Pressed, How To Obtain The Value Of Tkinter Entry And Pass It To The SQL Query?"