How to Take User Input and Store in Variable using Python Tkinter

In this section, we will learn how to take user input and store it in a variable using Python Tkinter. Also, we will cover these topics.

  • What are Variables in Python Tkinter
  • Entry Widget in Python Tkinter

Take user input using Entry Widget in Python Tkinter and Store in a Variable

In this section, we will learn about variables also we will understand the rules for creating variables Python. It is important to understand variables before we can learn to store the user input in a variable.

  • Variables are the reserved memory locations created with the purpose of storing values.
  • The computer remembers anything stored in a variable and that can be used anytime in the program. But it must be in scope.
  • There are few rules for creating variables:
    • Snake_case should be followed. That means the word should be divided using underscore(s). Example: player_name.
    • Variable must starts with lowercase or underscore. Example: _player_name, player_name
    • Only Letters, Numbers & Underscores are allowed in variables. No special characters except underscore are allowed.
    • Variables are Case Sensitive. Example: player and Player are two different variables.
    • Python has Reserved keywords like class, int, str, etc. One should not create variables with the name that is in the reserved keyword.

You may also like the following Python variable tutorials:

Entry Widget in Python Tkinter

Python Entry widget is the widely used widget as it serves the purpose of taking input from the user. So before we can store the input we need to learn how to take input from the user. You can check out the article on Entry Widget in Python Tkinter.

  • Entry Widgets are used to take user input.
  • While filling forms, the boxes where you enter details in an Entry Widget.
  • Command to create Entry Widget Entry(master).pack()
  • To store a value of the Entry Widget we use Variables. Simply provide any name before creating a widget and that name will store the value of the input.
  • Make sure the chosen name is following the variable rules as mention in the above section on the variable.
  • Later this stored value can be fetched using the get() method.
  • In the below program we have used all the things we have learned so far. We have used variables, the Entry widget, and the get method.

Code:

In this code, we have created a simple application wherein users need to provide a name for player registration. They will provide input in an Entry widget. The value will be stored in a variable named ‘player_name’.

After clicking on the button a function will be called that will pull the value from the Entry widget and will display it in a Label widget. The pulling of information from the Entry Widget is done using the get() method.

from tkinter import *

ws = Tk()
ws.title("PythonGuides")
ws.geometry('400x300')
ws['bg'] = '#ffbf00'

def printValue():
    pname = player_name.get()
    Label(ws, text=f'{pname}, Registered!', pady=20, bg='#ffbf00').pack()


player_name = Entry(ws)
player_name.pack(pady=30)

Button(
    ws,
    text="Register Player", 
    padx=10, 
    pady=5,
    command=printValue
    ).pack()

ws.mainloop()

Output:

In this output, name “nobita” is entered in the entry widget and same name is displayed with the message in the label widget when the button is clicked.

python tkinter store value in variable
Take User Input and Store in Variable using Python Tkinter

You may like the following Python tutorials:

In this section, we have learned about taking user input and storing a value in a variable also we have covered these topics.

  • What are Variables in Python Tkinter
  • Entry Widget in Python Tkinter