0

Ok so I have been trying to get user input from an entry with Tkinter and store it into a variable that is printed at the end of the program, but when the variable is printed, the output is the initial value.

The code:

from tkinter import *
hello = ''
root = Tk()

e = Entry(root, width = 50)
e.pack()
def myClick():
    hello = e.get()
button1 = Button(root, text = 'ik', command = myClick)
button1.pack()
root.mainloop()
print(hello)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • `hello` in the function `myClick` is not the same variable as `hello` on global. You can use `global` keyword in the function. – ywbaek May 16 '20 at 20:21
  • I use global like: global.hello = e.get()? –  May 16 '20 at 20:27
  • 1
    Read up on [Tutorial - 9.2. Python Scopes and Namespaces](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces) and [Tkinter understanding mainloop](https://stackoverflow.com/a/29158947/7414759) – stovfl May 16 '20 at 21:18

1 Answers1

0

The hello variable in myClick is a local variable. To get it to refer to the global variable you need to declare it as global.

def myClick():
    global hello
    hello = e.get()
RedKnite
  • 1,525
  • 13
  • 26