Daniel Morris Daniel Morris - 1 month ago 4
Python Question

How do I store a text entry as a variable in Tkinter?

So.....

I want my program to launch a Tkinter GUI Window, which gives the user text boxes to enter values as strings or as integers.....
The code below does that

from Tkinter import *


def add_text():
label1 = Label(root, text="You have entered the information to the average speed checker")
label1.pack()

root = Tk()
root.title("Average Speed Checker")
root.geometry("450x165")

veh_reg_label = Label(root, text="Vehicle Registration:")
veh_reg_label.pack()

veh_reg_text_box = Entry(root, bd=1)
veh_reg_text_box.pack()

distance_label = Label(root, text="Distance")
distance_label.pack()

distance_text_box = Entry(root, bd=1)
distance_text_box.pack()

time_label = Label(root, text="Time")
time_label.pack()

time_text_box = Entry(root, bd=1)
time_text_box.pack()

enter_button = Button(root, text="Enter", command=add_text)
enter_button.pack()

root.mainloop()


However.....
There is one problem: I can't seem to find a (simple ;) ) way of storing this text entry as a variable, which means that I am not able to continue with my program by making it calculate if a car is over a certain speed when travelling a distance of road etc etc.....

Answer

This seems to work:

def retrieve_input():
    global vehicle_registration
    global vehicle_time
    global vehicle_distance
    vehicle_registration = veh_reg_text_box.get()
    vehicle_time = time_text_box.get()
    vehicle_distance = distance_text_box.get()
    number_checker()
Comments