全局变量不起作用

时间:2016-07-25 16:28:52

标签: python loops variables global

我的全局变量在我的代码中不起作用。我对此很新,我似乎无法解决这个问题:我设置了变量(仅显示gna),可以通过相应按钮触发的输入字段进行操作。出于某种原因,它没有在循环中进行更改。我试图将其变为可以绘制更改变量的位置,但它给出了以下错误:

Exception in Tkinter callback Traceback (most recent call last):  
 File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 1549, in __call__
     return self.func(*args)   File "G:/PYTHON/Eulers.py", line 64, in graph
     v[i + 1] = 1 / c * (gna * f[i] - gk * u[i]) * del_t + v[i]
 TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('< U32') dtype('< U32') dtype('< U32')

以下是代码:

gna = 0.9

gnalabel = Label(topFrame, text="gna = %s" % gna)
gnalabel.pack()

gnaEntry = Entry(topFrame, justify=CENTER)
gnaEntry.pack()

def gnacallback():
    global gna
    gna = gnaEntry.get()
    gnalabel.config(text="C = %s" % gna)

gnaButton = Button(topFrame, text="Change", width=10, command=gnacallback)
gnaButton.pack()

def graph():
    global c, gna, gk, beta, gamma

    for i in range(0, len(t)-1):

        stinum = np.floor(i / 3000)
        stimt = 3000 + 3000 * (stinum - 1)

        f[i] = v[i] * (1 - (((v[i]) ** 2) / 3))
        v[i + 1] = 1 / c * (gna * f[i] - gk * u[i]) * del_t + v[i]

        if(i == stimt):
            v[i + 1] = v[i + 1] + v_stim

        u[i + 1] = (v[i] + beta - gamma * u[i]) * del_t + u[i]

    plt.plot(v)
    plt.show()

1 个答案:

答案 0 :(得分:2)

gna = gnaEntry.get()

Entry.get返回一个字符串,这可能是您在graph中执行的算术的不合适类型。请先尝试转换为数字。

gna = float(gnaEntry.get()) #or perhaps `int` if it's always an integer