Apr-23-2020, 09:29 AM
I have created a GUI where you can enter values (x values) manually. If you enter a value x1, trace-method will automatically calculate f(x1)=x1^2=y1 and mean(y) = (1/5 sum_{i=1}^{5} y_i). So every time an x-value is entered, the corresponding y-value and mean(y) is calculated.
The code below works. If you start it you get:
I would like to remove the initial values 0.0 from some cells. The window should look like this when the code is executed:
To get the desired result, I added at the very end before mainloop()
Do any of you know why the code with y_values[i].set(""), cells[(i,0)].delete(0,END) no longer works correctly and how to solve this problem?
Here is the full Code:
The code below works. If you start it you get:
I would like to remove the initial values 0.0 from some cells. The window should look like this when the code is executed:
To get the desired result, I added at the very end before mainloop()
for i in range(1,5): y_values[i].set("") cells[(i,0)].delete(0,END)If you start the code with this change, the program will not work properly anymore. If you enter an x-value, only the corresponding y-value is calculated, but not mean(y).
Do any of you know why the code with y_values[i].set(""), cells[(i,0)].delete(0,END) no longer works correctly and how to solve this problem?
Here is the full Code:
from tkinter import * import tkinter as tk root = Tk() Label(root, text = "x-values",padx = 10).grid(row = 0, column = 0) Label(root, text = "y-values",padx = 10).grid(row = 0, column = 1) Label(root, text = "Mean y", padx = 10).grid(row = 0, column = 2) # Create Variables x_values, y_values = ["x%d" % x for x in range(5)], ["y%d" % x for x in range(5)] for i in range (5): x_values[i], y_values[i] = DoubleVar(), DoubleVar() mean = DoubleVar() # Create Table rows, columns, cells = 5, 2, {} for i in range(columns): for j in range(rows): if i == 0: # x-values that can be entered b = Entry(root, textvariable=x_values[j]) b.grid(row = j+1, column = i, sticky = W + E) cells[(j,i)] = b else: # y-values that are computed by f b = Label(root, textvariable=y_values[j]) b.grid(row = j+1, column = i, sticky = W + E) cells[(j,i)] = b label_mean = Label(root, textvariable = mean).grid(row = 1, column = 2, rowspan = 5) # compute y-values def f(name, index, mode): try: for i in range(5): y_values[i].set(x_values[i].get()**2) except tk.TclError: pass # compute mean and standard deviation def statistic(name, index, mode): try: y_sum = 0 for i in range(5): y_sum += y_values[i].get() y_normalized = y_sum / 5 mean.set(y_normalized) except tk.TclError: pass # Traces to trigger the above functions for i in range(5): x_values[i].trace('w', f) y_values[i].trace('w', statistic) mainloop()