I have tried StringVar, I've tried set(). Note: The program successfully calculates the value and returns the answer in a seperate pop-up, but I would like it to display the answer in the readonly "yards" entry field as well.
import tkinter as tk
import tkinter.ttk as ttk
#Formats all of the visible elements of the tkninter GUI.Connects the buttons to their functions.
class MetersToYardsApp:
def __init__(self, parent):
topLevel = ttk.Frame(parent, padding=10)
topLevel.grid(column=0, row=0)
headerLabel = ttk.Label(topLevel, text="Meters to Yards Calculator", font="{Arial} 16 {bold}")
headerLabel.grid(column=0, row=0, sticky="nsew")
inputFrame = ttk.Frame(topLevel, padding=10)
inputFrame.grid(column=0, row=1, sticky="nsew")
metersLabel = ttk.Label(inputFrame, text="Meters:")
yardsLabel = ttk.Label(inputFrame, text="Yards:")
metersEntry = ttk.Entry(inputFrame)
yardsEntry = ttk.Entry(inputFrame, state="readonly")
metersLabel.grid(column=0, row=0, sticky="e")
yardsLabel.grid(column=0, row=1, sticky="e")
metersEntry.grid(column=1, row=0, pady=3)
yardsEntry.grid(column=1, row=1, pady=3)
buttonFrame = ttk.Frame(topLevel)
buttonFrame.grid(column=0, row=2, sticky='nsew')
clearButton = ttk.Button(buttonFrame, text="Clear", command=self.clear)
okayButton = ttk.Button(buttonFrame, text="Calculate", command=self.calculate)
clearButton.grid(column=0, row=0, padx=3)
okayButton.grid(column=1, row=0)
self.mainWindow = topLevel
self.metersEntry = metersEntry
self.yardsEntry = yardsEntry
#Clear Button
def clear(self):
# print("Clear")
self.metersEntry.delete(0, tk.END)
#Formats the Pop-up that displays the answer
def showAnswer(self, parent, text):
rootFrame = ttk.Frame(parent, padding=10)
rootFrame.grid(column=0, row=0)
headerLabel = ttk.Label(rootFrame, text="The Answer", font="{Arial} 14 {bold}")
headerLabel.grid(column=0, row=0)
answerLabel = ttk.Label(rootFrame, text=text, justify=tk.CENTER)
answerLabel.grid(column=0, row=1)
#Performs Calculations if input is valid.
def calculate(self):
# print("Calculate Meters: ", self.metersEntry.get())
try:
meters = float(self.metersEntry.get())
yards = meters * 1.094
except:
top2 = tk.Toplevel(self.mainWindow)
self.showAnswer(top2, "There was an Error.\n" "Please Enter a Valid Number for Meters")
# print("Please Enter a Valid Number for Meters.")
return
print(meters, "Meters is ", yards, "Yards. ")
top2 = tk.Toplevel(self.mainWindow)
self.showAnswer(top2, str(meters) " Meters is equivalent to " "{:.2f} Yards.".format(yards))
return float(yards)
CodePudding user response:
You do have to use DoubleVar to update the Entry element:
yards_entry_var = tk.DoubleVar(inputFrame, 0)
Then you should place it inside the Entry element 'textvariable':
yardsEntry = ttk.Entry(inputFrame, textvariable=yards_entry_var, state="readonly")
to use it outside the init function:
self.yards_entry_var = yards_entry_var
in the calculate function, after calculating the yards:
self.yards_entry_var.set(yards)
And finally, when you want to clear the value in the clear function:
self.yards_entry_var.set(0)