0

(Long Question)

I'm trying to write a piece of code that will take a file path from the user using a tkinter textbox when a button is pressed. It would then convert that textbox entry to a string attached to a normal python variable so I can use that variable in a function later to use that file. The code I attached can make the label copy the text box entry, but I cannot use that variable or myvar in "normal python code". Also, in the code I tried returning myvar.get() through the function mywarWritten, but I cant set a variable equal to the mywarWritten(parameters) because that is dependent on the textbox entry that doesn't happen until the button is pressed. When the button is pressed the print function works printing the statement but it doesn't return please when the function is set equal to attempt.

(In Short)

I want to take a value, or string, from the user using a Tkinter text box, and use the entry as a normal python variable. Preferably the value in the text box would only be taken when a button is pressed.

from Tkinter import *
import Tkinter as tk

root = Tk()
root.title("MyApp")

myvar = StringVar()

def mywarWritten(*args):
    print "mywarWritten",myvar.get()
    please = myvar.get()
    return please

#trying to make the function return the textbox entry but fails
attempt = mywarWritten()
print "plz %s" % (attempt)

#trying to just set it equal too but also fails
python_variable = myvar.get()
label = Label(root, textvariable=myvar)
label.pack()

text_entry = tk.Entry(root, textvariable=myvar)
button1 = tk.Button(root, text="Back to Home", command=lambda: mywarWritten()) 
button1.pack()
text_entry.pack()

#trying attempt and pythonvariable in "normal python code"
print attempt
print pythonvariable
root.mainloop()

Thanks for the help in advance.

4
  • function assigned to button can't return value because there is nobody who could receive this variable. Lines attempt = ... and python_variable = ... are useless because they are executed at start of script when Entry is still empy. You have to learn that only code inside functions assigned to Button or to other widgets make sense. Everything outside functions is executed only once - at start of script - and is used only to build GUI. Commented Dec 31, 2016 at 6:03
  • Thank you that makes a lot of sense. So instead of trying to bring the text entry out, I should bring my function (not shown) inside related to the button correct? Commented Dec 31, 2016 at 6:17
  • yes, you have to do everything inside functions. Commented Dec 31, 2016 at 6:19
  • Thanks furas I'm looking forward to trying this tomorrow morning. Commented Dec 31, 2016 at 6:25

1 Answer 1

3

You seem to have a few misunderstandings about scope, imports, references, and functions. myvar is already accessible, and all you have to do to access it is get() it. Don't import the same module multiple times, and try to avoid from x import *. Returning a value to a button doesn't make any sense and has no effect. Every line of code not in a function or class is executed immediately, so attempt = mywarWritten() and all of the other several times you did that outside a function will get the value of that StringVar as soon as the program runs, before there's anything in it. And lambda: func() is just func.

import Tkinter as tk

root = tk.Tk()
root.title("MyApp")

myvar = tk.StringVar()

def mywarWritten(*args):
    print "mywarWritten", myvar.get()

label = tk.Label(root, textvariable=myvar)
label.pack()

text_entry = tk.Entry(root, textvariable=myvar)
button1 = tk.Button(root, text="Back to Home", command=mywarWritten) 
button1.pack()
text_entry.pack()

root.mainloop()

Any time you want to access the contents of that entry widget, just do myvar.get() and there it will be.

You also have mywarWritten instead of my_var_written, with a v for var.

Overall, I very highly recommend you read the official Python tutorial (and use Python 3, because it's better than Python 2).

Sign up to request clarification or add additional context in comments.

1 Comment

I apologize for the simple errors. I had tried so much stuff it became a mess but I didn't want to delete it or I would've forgotten what I did. I tried clraning it up but clearly overlooked some stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.