自动计算 - 删除初始值
Automatic calculation - Remove initial values
我创建了一个 GUI,您可以在其中手动输入值(x 值)。如果你输入一个值x1,trace-method会自动计算
f(x1)=x1^2=y1 和 mean(y) = (1/5 sum_{i=1}^{5} y_i)
所以每输入一个x值,就会计算对应的y值和mean(y)。下面的代码有效。如果你启动它,你会得到:
我想从某些单元格中删除初始值 0.0。执行代码时 window 应如下所示:
为了得到想要的结果,我在mainloop()
之前的最后添加了
for i in range(1,5):
y_values[i].set("")
cells[(i,0)].delete(0,END)
我删除了某些单元格的初始值。如果您使用此更改启动代码,程序将不再正常运行。如果输入 x 值,则仅计算相应的 y 值,而不计算 mean(y)。
你们知道为什么带有 y_values[i].set("")
、cells[(i,0)].delete(0,END)
的代码不再正常工作以及如何解决这个问题吗?
这是完整的代码(来自图片 1):
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()
Mean 未计算,因为当您尝试将 None 值添加到 y_sum
时它引发异常。在 statistics
函数中添加 try
块。
def statistic(name, index, mode):
try:
y_sum = 0
for i in range(5):
try:
y_sum += y_values[i].get()
except:
pass
y_normalized = y_sum / 5
mean.set(y_normalized)
except tk.TclError:
pass
我创建了一个 GUI,您可以在其中手动输入值(x 值)。如果你输入一个值x1,trace-method会自动计算
f(x1)=x1^2=y1 和 mean(y) = (1/5 sum_{i=1}^{5} y_i)
所以每输入一个x值,就会计算对应的y值和mean(y)。下面的代码有效。如果你启动它,你会得到:
我想从某些单元格中删除初始值 0.0。执行代码时 window 应如下所示:
为了得到想要的结果,我在mainloop()
for i in range(1,5):
y_values[i].set("")
cells[(i,0)].delete(0,END)
我删除了某些单元格的初始值。如果您使用此更改启动代码,程序将不再正常运行。如果输入 x 值,则仅计算相应的 y 值,而不计算 mean(y)。
你们知道为什么带有 y_values[i].set("")
、cells[(i,0)].delete(0,END)
的代码不再正常工作以及如何解决这个问题吗?
这是完整的代码(来自图片 1):
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()
Mean 未计算,因为当您尝试将 None 值添加到 y_sum
时它引发异常。在 statistics
函数中添加 try
块。
def statistic(name, index, mode):
try:
y_sum = 0
for i in range(5):
try:
y_sum += y_values[i].get()
except:
pass
y_normalized = y_sum / 5
mean.set(y_normalized)
except tk.TclError:
pass