2

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!!

Hamza Lachi
  • 1,046
  • 7
  • 25

1 Answers1

2

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()
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
  • Now What can i type here? pg.typewrite(user_name, interval=0.2) pg means pyautogui – Hamza Lachi Aug 19 '19 at 06:43
  • Never used pyautogui. What is this line supposed to do? – Henry Yik Aug 19 '19 at 06:46
  • I want to type text so that's why I used pyautogui now what can I type here – Hamza Lachi Aug 19 '19 at 06:47
  • in chrome search bar – Hamza Lachi Aug 19 '19 at 06:47
  • I don't quite follow you. Your question is asking _How to save entry input to variable_, and it can be done by creating a `StringVar` and access it by `get` method. If you are not really asking for this, make it clearer in your question. – Henry Yik Aug 19 '19 at 06:49
  • @HamzaLachi I took a quick look at the docs of pyautogui. If I understand it correctly, you can associate it with the `trace` method of the `StringVar`. I have updated my answer on this. – Henry Yik Aug 19 '19 at 06:55
  • 1
    no problem bro i found my answer you i replace with this pg.typewrite(user_name, interval=0.2) from this: pg.typewrite(var.get(), interval=0.2) – Hamza Lachi Aug 19 '19 at 07:17