0

I have been trying to make a user interface (by using tkinter)in Python that saves the number you enter. In this case I want the user to be able to give a start value and an end value of a measurement, which afterwards will be implemented in a code. For this to work, I need the user interface to be able to store the values I enter. However, so far I haven't be able to export my variables with the assigned values. I only managed to get the variables with value 0. Could someone please help me figure out my mistake? Thanks in advance!

Here is the code I used:

import tkinter as tk
import tkinter.messagebox

root = tk.Tk()
tk.Label(root, text = "Start of measurement (index): ")
tk.Label(root, text = "End of measurement (index): ")

def add_text():
       label1 = tk.Label(root, text="You have entered the start and end index of the measurement")
       label1.pack()



Start = tk.DoubleVar(root)
End = tk.DoubleVar(root)

Start_measurement = Start.get()
End_measurement = End.get()

Start_label = tk.Label(root, text="Start of measurement (index): ")
Start_label.pack()

Start_text_box = tk.Entry(root, bd=1)
Start_text_box.pack()

End_label = tk.Label(root, text="End of measurement (index): ")
End_label.pack()

End_text_box = tk.Entry(root, bd=1)
End_text_box.pack()

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


root.mainloop()
1

2 Answers 2

1

It would be variable = Start_text_box.get() or variable = End_text_box.get().

Also to close the windows for later use in your code you need to do

root.quit()
root.withdraw()

For example:

def add_text():
    globals()['START']=Start_text_box.get()
    globals()['END']=End_text_box.get()
    print("Start was ", START)
    print("End was ", END)
    root.quit()
    root.withdraw()

The globals() is there because the START/END variables are defined within a local function so to use them elsewhere you need to assign them globally.

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

2 Comments

Thanks for your answer and explanation KJTHoward! I tried it and it worked :-)
@PallasAthena No problem, happy to help :). Feel free to mark this post as an answer to your question so the question is listed as answered and if anyone else has a similar problem they can find the solution.
1

In your add_text function you could first get the string of the textbox:

val1 = Start_text_box.get()

Then convert it to double:

val1 = float(val1)

Then print it

label1 = tk.Label(root, text="You have entered the start {0} and end index of the measurement".format(val1))

1 Comment

Thank you for your advice Toto, the code works now :-)

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.