1

I don't understand the reason why if I try to pass a tkinter entry's value between two python scripts, I get '!entry' instead the value.

First script:

from tkinter import *
w=Tk()
def send(e):
    import snd
e=Entry(w)
e.pack()
b=Button(w,command=lambda:send(e.get()))
b.pack()
w.mainloop()

Second script:

from __main__ import *
print(e)
pppery
  • 3,731
  • 22
  • 33
  • 46
Tommy
  • 101
  • 8
  • 2
    Perhaps you could consider putting a function inside your second script rather than relying upon `import snd` to do the correct thing. Import your `snd` module at the start of your first script and then call the function contained inside `snd` from your `send` function and pass it the value of e. – scotty3785 Jun 23 '22 at 15:02
  • 3
    Also get rid of the `from __main__ import *` from your second script. The reason you get `.!entry` is that when you import from main you are getting the value of `e` which in global scope of your first script is an entry widget rather than the `e` inside the `send` function. – scotty3785 Jun 23 '22 at 15:06

1 Answers1

0

I'm sorry if this is unhelpful, but you don't exactly need to send it. You can do this:

# file1.py
from tkinter import *
w=Tk()
e=Entry(w)
e.pack()
w.mainloop()

And:

# file2.py
from file1 import e
print(e.get())

This seems to work just fine.

EDIT: If you need a Button, you can do this:

# file1.py
# --snip--
def callback():
  text.set(e.get())
# --snip--
text = StringVar()
btn = Button(w, text="Save", command=callback)
btn.pack()
# --snip--

And:

# file2.py
from file1 import text
# Do what you want with the StringVar text
# You can convert it to a string with:
# txt = text.get()
OmegaO333
  • 57
  • 8
  • thanks for answer, but it doesn't work without button, anyway i always get same error – Tommy Jun 30 '22 at 08:17
  • @Tommy If you need to send, I can't help you. I've updated my answer with a button. If this still doesn't work, i don't know what else to do. – OmegaO333 Jul 01 '22 at 17:11