Search code examples
pythonvariablestkintertkinter-entry

How to save entry input to variable in tkinter i'm using python 3


How to save entry in entry variable my question is how to save entry into a variable?

FOR EXAMPLE:

user_name = Entry(frame, bd =5)
user_name.pack(side = RIGHT)

If a user types any data I want to save data in this variable like input:

user_name = input("Enter Your UserName!: ")
pg.typewrite(user_name, interval=0.2)

Now he is typing but when we changed into entry this script is not typing for example:

user_name = Entry(frame, bd =5)
user_name.pack(side = RIGHT)
pg.typewrite(user_name, interval=0.2)

Now he is not typing!!


Solution

  • You can create a tkinter variable like StringVar and set it as the textvariable of your Entry widget. Then you can access the content by calling the get method on the variable.

    import tkinter as tk
    
    root = tk.Tk()
    var = tk.StringVar()
    user_name = tk.Entry(root,textvariable=var)
    user_name.pack()
    #var.trace("w",lambda *args: print (var.get()))
    var.trace("w", lambda *args: pg.typewrite(var.get(), interval=0.2)
    
    root.mainloop()