Reputation: 3
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()
Upvotes: 0
Views: 152
Reputation: 876
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.
Upvotes: 1
Reputation: 89
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))
Upvotes: 1