在开头w和c等于0,但问题是当我用比例小部件改变w的值时,然后我改变c的值,w的值回到0。
我该如何解决这个问题?
这是我的代码:
import Tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
app = tk.Tk()
app.wm_title("Tools")
def updateScale(value):
ax.clear()
print "scale is now %s" % (value)
ax.plot(m, np.exp(m))
a = np.array([1, 2, 3])
w = float(value)
b = w*a+c
ax.plot(a,b)
graph.show()
return w
def updateScale1(value1):
ax.clear()
print "scale is now %s" % (value1)
ax.plot(m, np.exp(m))
a = np.array([1, 2, 3])
c = float(value1)
b = w*a+c
ax.plot(a,b)
graph.show()
return c
fig = Figure(figsize=(6, 4), dpi=96)
ax = fig.add_subplot(111)
#bx = fig.add_subplot(111)
m = np.array([1,2,3])
ax.plot(m,np.exp(m))
a = np.array([1, 2, 3])
c = 0
w = 0
b = w * a + c
ax.plot(a,b)
graph = FigureCanvasTkAgg(fig, master=app)
canvas = graph.get_tk_widget()
canvas.grid(row=0, column=0, rowspan = 11, padx =10, pady =5)
value = tk.DoubleVar()
scale = tk.Scale(app, variable=value, orient="horizontal",length = 100, from_=0.55, to=2.75, resolution = 0.01,command=updateScale)
scale.grid(row=0, column=1)
value1 = tk.DoubleVar()
scale1 = tk.Scale(app, variable=value1, orient="horizontal", command=updateScale1)
scale1.grid(row=1, column=1)
app.mainloop()
答案 0 :(得分:0)
这是因为更新功能使用c
和w
的未更新值。可能有更好的方法,但尝试添加:
global c, w
在每个更新函数定义中。
您的最外层范围包含变量c
和w
,但函数中的变量c
和w
是完全不同的变量,因为它们不是您的outerscope的子项也没有明确地将变量传递给它们。