Skip to content Skip to sidebar Skip to footer

Getting A Tkinter Input Stored Into A String Variable In The Next Function?

I am new to TKinter and I am not able to figure out how to store the input from a textbox in TKINTER. I've tried following virtually every tutorial and looked at similar posts but

Solution 1:

There is built-in function named input, try not to use it as a variable name. Other than that, it is pretty straight-forward,

You assign a Variable Class of your choice (StringVar() in here) for Entry then get content of said variable any time you want with get() method.

Also there is a get() method for Entry. With that, you can get content of Entry without using StringVar.

Below is a simple example showing how to do it. You should implement it to your code yourself.

import tkinter as tk

def get_class():  #no need to pass arguments to functions in both cases
    print (var.get())

def get_entry(): 
    print (ent.get())


root = tk.Tk()

var = tk.StringVar()

ent = tk.Entry(root,textvariable = var)
btn1 = tk.Button(root, text="Variable Class", command=get_class)
btn2 = tk.Button(root, text="Get Method", command=get_entry)

ent.pack()
btn1.pack()
btn2.pack()

root.mainloop()

EDIT: By the way, next time when you post a question, please consider adding the full traceback or what went wrong (what did you expect and what did you get etc..) instead of saying "it is not working" only. With that, you will probably get more help with more precise answers.

Post a Comment for "Getting A Tkinter Input Stored Into A String Variable In The Next Function?"